Elm Type Annotation

This is from the book Programming Elm,

main : Html msg
main =
div [] [ text “Picshare” ]

The book does not clearly explain the mains’s type annotation, does somebody have a better explanation?

I believe this belongs in the other languages section


Yea the Html type is parameterized by msg. Which is to say that it can emit messages of type msg. Here msg is abstract because the html is not sending any. You could write it as main: Html None

1 Like

please expand explanation, it’s still confusing.

Ok, so the main is a function that takes no parameters in this case. it will return a data structure that will be rendered as HTML (hence the HTML). But in Elm html events are also typed. So you can say this block of HTML will only put out this list of types (which are handed elsewhere) so we have to let the type system know what events the HTML will put out.

If thats does not do it let me know what part you find confusing


1 Like

The author of the book says, “The text function is crucial here. You can’t use a lone string because the Elm
type system wouldn’t accept it. Look at the type annotation for main to understand further.”. So Html is of type variable msg, which infers it can only hold events? Which is confusing because I think Html is of type DOM.

Ok the text function has the type “String -> Html msg” which turns a string into a Html Dom node. (Or at least something that will become one) the Html type can hold any Html, but the msg parameter tells it what types of events it can emmit, for example if you click on it it could emit an event saying that something was clicked on.

These two videos might help you



1 Like

That should be Never not None (been a while since I have done Elm)

@zkessin msg is a parameter to Html? I though only functions can accept parameters. ALso the uathor confused when he says “Look at the type annotation for main to understand further.” should have been look at type annotation for text.

No types can have parameters too. For example the type List is actually List a where a is a generic type. So you can write a function that takes a generic list and pass it a list of strings or a list of integers or a list of any other type.

1 Like