Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Confused about "Html msg"

Tags:

elm

Consider this snippet:

type alias Model =
    { x : Int }


testFunc : Model -> Html String
testFunc model =
    div [] []

I am very confused here. div is a function that returns a Html msg. But testFunc returns Html String.

How does this compile? Am i missing out on some very basic understanding here?

like image 692
user3282666 Avatar asked Jan 13 '17 07:01

user3282666


1 Answers

You can kind of treat msg like Generics in Object Oriented programming. This is not technically correct, though the concepts are similar. Just keep in mind that generic is not the correct terminology strictly speaking.

With that said, div is a function that returns a value of Html with a 'generic' type of msg (where msg could be any type). This would be written as Html<A> in Java or C#, where is A is a placeholder for any type.

Elm has type inference, so when the return type of testFunc is Html String Elm infers that msg must therefore be a String type.

Also note Elm requires the generic type be lower case (e.g msg). It's often confusing in Elm code as people will often define a real type of Msg and also name the generic placeholder type msg.

like image 112
Pete Avatar answered Oct 19 '22 23:10

Pete