You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
A bit of a crazy idea, but is it worth considering defining a Class a literal value? Like a method with newClass(parent="foo") in its signature would only be dispatched to by the exact value "foo". This would be useful, for example, when conditioning on a file extension, or the value of "method" parameter. Essentially it would be using dispatch to implement switch/case statements. In the past, I've done something like new(paste0(fileExt, "File")) to implement this.
@lawremi do you still think this is important? My sense is that we should just close it, and wait to see if it arises in real life.
I still think switchClass() is a cool idea, but implementing it at the package level is awkward because you'd have to use NSE to get the object on the LHS of :=/=.
@hadley@lawremi Could literal types (specifically unions of literal types) be used to specify that a property must be in a finite set of possible values, like in TypeScript? Essentially replacing the common match.arg() constructions? So you could do e.g.:
Enums would be nice, especially if there was a switch or “match” statement to make using them a bit more ergonomic than just if-statements. But they might be quite cumbersome for the match.arg() replacement case.
You’d need to define the enum elsewhere first, and then handle converting the enum variants into strings, rather than being able to just use the property directly. I’m imagining usage would look something like this:
(And actually it should probably be like class_literal_union("GET", "POST") instead of new_union("GET", "POST") so that as_class("GET") doesn't have to work.)
I don't understand why your proposal couldn't be made to work with enum() instead of new_union(). But I think it'd be better to discuss this in a new issue. Do you mind opening one with a summary of your proposal?
I'm assuming enum members/variants should be considered opaque, as in it doesn't matter what the underlying value is. That would make them only useful for control flow, and since we don't have good constructs for control flow with arbitraty values (only really switch() which works with strings), that route could be cumbersome.
Oh I see what you mean now. I feel like there will be enough trade-offs (i.e. you can no longer see the possible options in the function signature, and two sets in one function will either need multiple dispatch or multiple generic helpers) that it'll only be worthwhile in limited situations, and so probably not something we'd want to add to R7 at this point.
@hadley I think in the (in my view) primary use case of validating class properties declaratively, literal classes would actually offer a big benefit in terms of being able to see the possible options. As for dispatch, I would see it as a fairly rare use-case to have a function that would dispatch on one, let alone multiple, literal union arguments.
I spent some time summarising the benefits in this new issue in #267, I hope you find it useful.
Activity
hadley commentedon Oct 7, 2020
It occurred to me that we could also offer the flip side of this — a switch-style statement that uses dispatch:
hadley commentedon Oct 22, 2021
@lawremi do you still think this is important? My sense is that we should just close it, and wait to see if it arises in real life.
I still think
switchClass()is a cool idea, but implementing it at the package level is awkward because you'd have to use NSE to get the object on the LHS of:=/=.lawremi commentedon Oct 23, 2021
Not very important, we can close.
mikmart commentedon Dec 3, 2022
@hadley @lawremi Could literal types (specifically unions of literal types) be used to specify that a property must be in a finite set of possible values, like in TypeScript? Essentially replacing the common
match.arg()constructions? So you could do e.g.:Instead of:
hadley commentedon Dec 3, 2022
If we did want to support that, I think I'd prefer an explicit enum type.
mikmart commentedon Dec 3, 2022
Enums would be nice, especially if there was a switch or “match” statement to make using them a bit more ergonomic than just if-statements. But they might be quite cumbersome for the
match.arg()replacement case.You’d need to define the enum elsewhere first, and then handle converting the enum variants into strings, rather than being able to just use the property directly. I’m imagining usage would look something like this:
Versus with literal unions:
mikmart commentedon Dec 4, 2022
I made a “proof of concept” branch to test this out:
And generics:
@hadley does this impact your thinking on this? Personally I think it's pretty expressive.
mikmart commentedon Dec 4, 2022
(And actually it should probably be like
class_literal_union("GET", "POST")instead ofnew_union("GET", "POST")so thatas_class("GET")doesn't have to work.)hadley commentedon Dec 4, 2022
I don't understand why your proposal couldn't be made to work with
enum()instead ofnew_union(). But I think it'd be better to discuss this in a new issue. Do you mind opening one with a summary of your proposal?mikmart commentedon Dec 4, 2022
I'm assuming enum members/variants should be considered opaque, as in it doesn't matter what the underlying value is. That would make them only useful for control flow, and since we don't have good constructs for control flow with arbitraty values (only really
switch()which works with strings), that route could be cumbersome.I'll try to summarise in a new issue.
hadley commentedon Dec 4, 2022
Oh I see what you mean now. I feel like there will be enough trade-offs (i.e. you can no longer see the possible options in the function signature, and two sets in one function will either need multiple dispatch or multiple generic helpers) that it'll only be worthwhile in limited situations, and so probably not something we'd want to add to R7 at this point.
mikmart commentedon Dec 4, 2022
@hadley I think in the (in my view) primary use case of validating class properties declaratively, literal classes would actually offer a big benefit in terms of being able to see the possible options. As for dispatch, I would see it as a fairly rare use-case to have a function that would dispatch on one, let alone multiple, literal union arguments.
I spent some time summarising the benefits in this new issue in #267, I hope you find it useful.