Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Understanding generic union types in Elm

Tags:

elm

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:

  1. How does the div function construct one of these objects?
  2. How does an object like this get used?
  3. How could an object like this get used?
  4. Is there any value for a user to define a type like this, or is Html msg just a magical type to support the Elm compiler/runtime?
like image 259
yxrkt Avatar asked Jul 25 '16 02:07

yxrkt


1 Answers

Html msg is a Type Alias for Node msg, which is a Generic Union Type

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

How div function uses generic Union Types

Thanks 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
}

How you can use generic Union Types

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 Types

Html 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)
like image 177
halfzebra Avatar answered Oct 25 '22 15:10

halfzebra