-
Notifications
You must be signed in to change notification settings - Fork 118
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Clarify union types #151
Comments
Hey @lazarljubenovic, I know this answer is coming in a year later but hopefully it still helps. At least anyone else reading this later will have an explanation. Another way to think of Union types is to think of them as sets. The set of all integers is [-infinity ... -1, 0, 1, 2, 3, ..., +infinity] If you were to model this in elm, the type would be. type Integer = -99999 | -99998 | ... | -3 | -2 | -1 | 0 | 1 | 2 | 4 | ....9999999 I don't know if this would even compile but this is how you should be thinking of Union types. The point is, left hand side of the So an integer can be -9999, or 0, or 84894, or whatever else is in the "set" (or more precisely, "type") called Here is the type definition for type Maybe a = Just a | Nothing This is useful when you are not sure if the a value exists at all. hasAValue : Maybe Int -> Bool
hasAValue unknown =
case unknown of
Nothing -> False
case Just value -> True
-- hasAValue Maybe Nothing -> False
-- hasAValue Maybe 12 -> True Another realistic example would be a linked list: Here's the type definition in elm for a Singly Linked List type LinkedList a =
Null
| Node a (LinkedList a) So a linked list can be either Notice that I defined
I could have been more strict and defined it as anyways ... So if we had a
And the values would look as follows: myList : LinkedList Int
myList = Node 12 (Node 99 (Node 5 (Null))) Hope I didn't scare anyone off with the fact that LinkedLists are defined recursively! @sporto Let me know if you would like me to submit a PR based on my explanation here. |
Thanks for the reply! 😄 Elm is for years something that I want to learn, plan to learn, I preach it to everyone but I have never really sat down and did something serious. Here am I, a year later, still on the same level, with no new experience, yet thinking about Elm every week :( Anyway, my memory is a bit rusty by this point (obviously), but reading my question and your answer, I still didn't catch the aswer. I specifically ask about I get what a union type is, but I just couln't wrap my head around what exactly could |
@lazarljubenovic I will try to explain the way I understand what these are. Many languages let you define an "atom" type. e.g. in ruby you can use symbols e.g.
However in Elm you can reuse the same name in different modules. e.g in one module you can have |
I'm not sure if I'm missing something obvious, but the section on Union types starts mentioning
Yes
andNo
, and keeps dragging it through all examples without ever explaining what this could be.Having never written anything in Elm myself and this being my first time going though the tutorial, I cannot imagine what would type
Yes
andNo
be.I understand the basic idea about union types, but the concrete example looks like it could use some improving.
The text was updated successfully, but these errors were encountered: