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?
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
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With