Confusedly about "HTML msg"
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 an Html String .
How does this compile? Did I miss some very basic understanding here?
You can refer to msg as generics in object oriented programming . This is not technically correct, although the concepts are similar. Just keep in mind that general is not the right terminology, strictly speaking.
With that said, div is a function that returns an Html value with the "common" type msg (where msg can be any type). This will be written as Html<A> in Java or C #, where A is a placeholder for any type.
Elm has type inference , so when the return type of testFunc is Html String Elm indicates that msg should be a String .
Also note that Elm requires the generic type to be lowercase (e.g. msg ). This is often confusing in Elm code, as people often define the real type of msg , and also call the generic placeholder type msg .
Consider the difference between lowercase and uppercase definitions. The msg in a signature of a div is a generic type. It could be called a or something else that starts with lowercase.
In your definition of testFunc you simply use a div with a String return type, which is not a problem for the elm compiler: this could very well be your msg type.
In other words, the return type of the div not Html Msg , but Html Msg instead. This allows you to use it with any messages that your component identifies, and makes it truly universal.