I'm having some trouble understanding what exactly the Html msg
type is, or how it gets used. I found this line of code in VirtualDom.elm, which Html msg
seems to be an alias of:
type Node msg = Node
This looks like a generic union type with one type parameter, msg
, and one trivial case that holds no additional information. I'm wondering:
div
function construct one of these objects?Html msg
just a magical type to support the Elm compiler/runtime?Html msg
is a Type Alias for Node msg
, which is a Generic Union Type
Node msg
It only makes sense to reason about Node msg
type signature in a context of Elm Architecture, where all input for your application, except flags for Http.App.programWithFlags
, is happening through messages.
msg
type variable in Node msg
is only hinting you towards the idea, that all messages from your DOM sub-tree should belong to a single union type.
node
: String
-> List (Attribute msg)
-> List (Html msg) -- List of children nodes with a msg
-> Html msg -- Produced DOM node
div
function uses generic Union TypesThanks to msg
type variable, Elm compiler knows, when your DOM tree is correct in the context of Elm Architecture.
In JavaScript world Node
value of Node msg
Union Type is representing the object with the following structure:
{
"type":"node",
"tag":"div",
"facts":{}, // Attributes and DOM events
"children":[], // Children, have the same structure
"descendantsCount": 0 // Used for Virtual DOM diffing
}
Most of the complex core data structures are implemented using generic Union Types, check Maybe or Dict to get inspired.
Html msg
and user-defined generic Union TypesHtml msg
in particular is somewhat magical, but you can implement some interesting structures, like linked lists or other tree-like structures.
type List a =
Empty | Node a (List a)
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