Reputation: 213
What is the meaning of this Elm example's main function type annotation?
main : Html msg
main =
div [] [ text "1" ]
What is Html and msg?
Upvotes: 2
Views: 1334
Reputation: 3725
First of all, that one is not a function, because it doesn't take any parameter.
However, type annotation is saying that the main
is of type Html
with a not yet known variant msg
.
Html
is the type, like Int
or String
, and msg
is the type variant.
Some types can have variants, take List a
as an example.
List a
means that you are declaring a List
where the type variant is irrelevant or not yet known. For instance, the type annotation for List.length
method is:
length : List a -> Int
When you'll use this method, you'll certainly have a type instead of a
, like String
or Int
or whatever, by the behaviour of length
doesn't change.
Don't be confused then by the msg
addition to Html
. It has the same meaning. In Elm you render html code that may dispatch some messages, and you usually define them as a union type like:
type Msg
= ClickedSave
| EnteredText String
So, your main declaration can remain Html msg
, or Html a
until you dispatch a message.
If you, for instance, add an onClick
handler to your div
like:
main =
div [ onClick ClickedSave ] [ text "1" ]
the type annotation becomes (whether you annotate it or not) Html Msg
, because now the message you are dispatching is of type Msg
.
Hope it's clear, and sorry for my english, it's not my native language.
Upvotes: 9