Robin Heggelund Hansen
2017-08-02 14:00:02 UTC
I think most people find that Union Types are a wonderfull thing. I would
argue that much of the beauty of Union Types comes from using them in a
case-of statement, having the compiler complain when there is a case one
haven't covered.
Strings and numbers are not so wonderfull, because they span essentially
unlimited values, so case-of statements become less valuable.
However, strings and numbers still need to be used in production apps for
several reasons:
1) Union Types is not supported in Json, so one needs to convert to/from
strings or numbers for communicating with other systems.
2) Union Types are not comparable, and so cannot be used as keys in Sets or
Dicts.
So a server might send me some json where the current user language is
represented by an Int (0 and 1, norwegian and english) and the current
game being played is a string ("chess", "checkers", "reversi"). It would be
great to have some compiler help for these values (say, you forgot the case
statement for "reversi"), AND it would be great to avoid writing
encoders/decoders for the union types and converting back and forth several
places.
Enums would help with this.
The way I imagine this to work is that an enum essentially represents a
restricted set of String or Number. Once the compiler sees that you're
working with an enum, it could help you out in case statements. And since
an enum is just a string, or just a number, they can be used in the same
places where one would normally use them, like as a value in a select.
One would need a way to check that a string/number matches the values of an
enum though. Something like `Enum.matches : MyStringEnum -> Maybe (Enum
MyStringEnum)`.
The one thing I can think of which makes this a bad addition to Elm, is
that it might be a bit confusing for beginners when to use enum and when to
use unions... maybe.
Anyway, what do people think of this?
argue that much of the beauty of Union Types comes from using them in a
case-of statement, having the compiler complain when there is a case one
haven't covered.
Strings and numbers are not so wonderfull, because they span essentially
unlimited values, so case-of statements become less valuable.
However, strings and numbers still need to be used in production apps for
several reasons:
1) Union Types is not supported in Json, so one needs to convert to/from
strings or numbers for communicating with other systems.
2) Union Types are not comparable, and so cannot be used as keys in Sets or
Dicts.
So a server might send me some json where the current user language is
represented by an Int (0 and 1, norwegian and english) and the current
game being played is a string ("chess", "checkers", "reversi"). It would be
great to have some compiler help for these values (say, you forgot the case
statement for "reversi"), AND it would be great to avoid writing
encoders/decoders for the union types and converting back and forth several
places.
Enums would help with this.
The way I imagine this to work is that an enum essentially represents a
restricted set of String or Number. Once the compiler sees that you're
working with an enum, it could help you out in case statements. And since
an enum is just a string, or just a number, they can be used in the same
places where one would normally use them, like as a value in a select.
One would need a way to check that a string/number matches the values of an
enum though. Something like `Enum.matches : MyStringEnum -> Maybe (Enum
MyStringEnum)`.
The one thing I can think of which makes this a bad addition to Elm, is
that it might be a bit confusing for beginners when to use enum and when to
use unions... maybe.
Anyway, what do people think of this?
--
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 elm-discuss+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
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 elm-discuss+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.