I think the problem here is that you're using types as an Enum. There's a bit of a difference between types and Enums.
Enums are a set of symbolic values that limit the values that variables of that Enum can take on. Types can act like Enums like you've shown, but they can also be functions. So they are really a different animal. What's really missing is a way to limit values a variable can take on at the *language level*. (Then you could use a *case* statement with the variable). I'm no language expert, but I suspect there are languages that have some form of *constraint-based types*. This is the piece that's missing in Elm. I suspect that this feature is either very difficult to do or a low priority since its use case is rare or maybe it's not even on Evan's radar. Whatever the case may be, I've have always wanted *constraint-based types* and would love to see a language properly implement this (assuming it wouldn't bloat the language unnecessarily). On Tuesday, May 2, 2017 at 8:44:34 PM UTC-7, Matthew Buscemi wrote: > > I have run into a problematic use case a couple of times, and based on > Slack discussions earlier today, it seems I'm not alone. > > The situation occurs when, for some reason, I need to maintain a list of > all constructors of a union type. The example given in Slack by mltsy: > > type State = Alabama | Alaska | Arizona | Arkansas | ... > > allStates = [ Alabama, Alaska, Arizona, Arkansas, ...] > > stateSelectBox : List State -> Html Msg > stateSelectBox states = > let stateValues = List.map toString states > in ... > > In the example above, simply turning allStates into a list of strings > would be fine for this particular use case, but in the context of a larger > system, it could be inadequate–other functions besides stateSelectBox may > want to utilize the power of performing a case over the union type. > However, the above solution is also structurally undesirable–if a new type > is added to State, then the programmer must also remember to update > allStates separately. The duplication and tight coupling are disconcerting, > and yet I can't see a viable alternative that preserves the power of types > without incurring the duplication. > > I have wondered if a language-level construct that takes a union type and > returns a list of all constructors for that type would be appropriate for > Elm. Just a thought. > > - Matt > > > -- You received this message because you are subscribed to the Google Groups "Elm Discuss" group. To unsubscribe from this group and stop receiving emails from it, send an email to [email protected]. For more options, visit https://groups.google.com/d/optout.
