This is a good explanation, Joey. I hope it's clear for Justin now. When I started Elm I had same problems understanding what is what in type declarations.
Let's summarize: type X = A a | B | C | X What do we have here: we are defining a type named "X" and 4 special functions called type constructors. These functions are the only thing on earth capable of producing "the thing of type X". The are: A, B, C and X. Notice the name of a function used to create "the thing of type X" can be, but does not have to be, named the same as the type itself. It's actually very rare, but you should know the difference and the fact that's possible. Also, the function "A" takes one argument of arbitrary type while the other type constructors takes none. The "a" in "A a" can be something like "String" for example. Or "List String". Or "Result Http.Error String". When "a" is something like this, you have to put it in the parenthesis or compiler would think you are providing more parameters to "A" instead of "List" or "Result", you know what I mean: A (List String) vs A List String. Regard, Witold Szczerba On Fri, Nov 25, 2016 at 11:44 PM, Joey Eremondi <[email protected]> wrote: > But are Cmd/Sub/Html also types ? > > > They are type constructors. List Int is a type, but List is not a type. > It's a thing that makes a type when you give it a type. A type constructor! > > What these are is types which take a parameter. In this case, the > parameter is the message type. For example, Html Msg means that this is an > Html value, which produces values of type Msg, that will eventually make > their way to your update function. > > type Msg = MorePlease | NewGif (Result Http.Error String) > > > Result is also a Type Constructor! Specifically, a Result Error String is > a value which, when successful, produces a string, and on error, produces > an Http.Error. > You can see more in the Result library. Particularly interesting are > Result.map and Result.andThen, for chaining possible failing computations > together, and Result.withDefault, which extracts the result from a > successful result, or produces the default value. You can also pattern > match on them, with the Ok and Err constructors. > > http://package.elm-lang.org/packages/elm-lang/core/latest/Result > > Why does complier fail if I change Msg to 'type Msg = NewGif | MorePlease >> (Result Http.Error String)' ? > > > What the declaration of the tagged union here is saying is "NewGif is a > constructor that takes 0 arguments, and MorePlease takes one argument, > which is a Result that is either a String, or an Error). > In your original version, MorePlease took no arguments, and NewGif took 1 > argument. So wherever you're using them in your code, they probably are > either taking the wrong number of arguments, or you're pattern matching on > the wrong number of values. > > There's a lot of information, so feel free to ask more questions if this > leaves anything unclear! > > On Thu, Nov 24, 2016 at 5:17 AM, Justin <[email protected]> wrote: > >> Hi, >> >> Looking at the following >> >> http://elm-lang.org/examples/http >> >> I pretty much grok this, but have a couple of (likely dumb) questions - >> >> 1) When a function returns 'Cmd Msg', or 'Sub Msg', or 'Html Msg', what >> kind of a 'thing' is being returned ? >> >> Obviously Msg is a type. But are Cmd/Sub/Html also types ? (I suspect >> not). And when you prefix then to Msg is the resulting thing also a type ? >> (suspect not). A list of types ?? (suspect not). A closure or a deferred or >> something ? Am guessing more likely >> >> 2) type Msg = MorePlease | NewGif (Result Http.Error String) >> >> What's with the '(Result Http.Error String)' at the end ? I read the bit >> about union types but couldn't find anything relating to args in round >> brackets. If someone could point me to the relevant docs that would be >> great. >> >> 3) Why does complier fail if I change Msg to 'type Msg = NewGif | >> MorePlease (Result Http.Error String)' ? >> >> Don't NewGif and MorePlease require the same HTTP calls ? >> >> Elm looks great. Thanks in advance for any help and suggestions >> >> Justin >> >> -- >> 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. >> > > -- > 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. > -- 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.
