I would recommend having another read through the "Linked List" and
"Generic Data Structures" sections of the union types
<https://guide.elm-lang.org/types/union_types.html> page. The types you are
asking questions about behave the same way that the "List a" does.

1) Msg is a type. "Cmd Msg", "Sub Msg", and "Html Msg" are also types.

But without the Msg, "Cmd", "Sub", and "Html" are incomplete types. Let's
look at the "List a" type in the guide. You can have a value with "List
Int" or "List String" or "List Msg". The second word is required, but it
can be any type.


2) The parentheses are just for grouping.

If we write "NewGif (Result Http.Error String)", that means that NewGif is
a type constructor that takes 1 argument, and that argument has the type
"Result Http.Error String".

If instead we write "NewGif Result Http.Error String", that means that
NewGif is a type constructor that takes 3 arguments, of types Result,
Http.Error, and String. This would not compile, because Result is not a
type.

"Result Http.Error String" is a parameterized type, just like List a. The
difference is that Result needs 2 type variables filled, not just one. The
type definition for Result is

type Result a b = Err a | Ok b


3) If you change the type definition that way, then you need to change all
of the code in your program that is using the "NewGif" and "MorePlease"
type constructors. That means rewriting the update function, the view
function, and the getRandomGif function.



On Fri, Nov 25, 2016 at 2:01 AM, Justin <justin.worr...@gmail.com> wrote:

> Hi,
>
> I have some questions re the Elm http example
>
> http://elm-lang.org/examples/http
>
> I pretty much grok this, but
>
> 1) What type of a thing are 'Cmd Msg', 'Sub Msg', 'Http Msg' ?
>
> Obv 'Msg' is a type. 'Cmd', 'Sub', 'Http' sound from the docs like tasks
> the Elm runtime is going to perform; so is (eg) 'Cmd Msg' something like a
> function or a closure which is passed to the runtime for execution ?
>
> 2) type Msg = MorePlease | NewGif (Result Http.Error String)
>
> What role exactly do '(Result Http.Error String)' play in this union type
> definition ? Have read the stuff on union types but can't find anything
> related to 'round bracket args' there. Or maybe I have this all wrong ?
>
> 3) Why can't I reverse the structure of the union type ?
>
> type Msg = NewGif | MorePlease (Result Http.Error String)
>
> gives me a compiler error. But not clear why, as the NewGif and the
> MorePlease messages seem to be doing the same thing -> making an HTTP call.
>
> Elm looks great. Thanks in advance for anyone's help
>
> 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 elm-discuss+unsubscr...@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+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to