Understanding Elm Compound Types

I am having problems wrapping my head around the types of unions in elms. I understand a simple usage example like

type Visibility = All | Active | Completed 

That means the Visiblity value can be All, Active or Completed. So far, so good. However, when I got confused,

 type Msg = OnFetchMails (WebData (List Mail)) | OnFetchSmss (WebData (List SMS)) 

How should I understand that? Does this mean that Msg can be a function like OnFetchMails that accepts a function like WebData that accepts a list of letters? Or how should I understand this? I don't think (WebData (List Mail)) is a payload?

It's funny that I can make it work without understanding it

+5
source share
2 answers

When determining the type of union, you indicate all methods of constructing the value of this type. In its simplest form, this definition is as follows:

 type Visibility = All | Active | Completed 

As you might guess, this declares a Visibility type and defines three values, all of a Visibility type. The only way to build a value of type Visibility is to use one of these three options. Because of this, we often call them "constructors."

Here is a slightly more complex definition of the type of connection:

 type TrainStatus = OnTime | Delayed Int 

As you would expect, this defines two new “constructors,” OnTime and Delayed . But look at their types:

 OnTime : TrainStatus Delayed : Int -> TrainStatus 

The OnTime constructor takes null arguments, and this is just a value; it is already equal to TrainStatus . But Delayed declared as a constructor with one argument: it is a function that creates a new TrainStatus from Int . Thus, Delayed 5 , Delayed 10 and Delayed 100 are valid TrainStatus values. (We can interpret them as “5 minutes delayed” or something like that.)

The constructor may take several arguments; for example, if we want to include the reason for the delay as String:

 type TrainStatus = OnTime | Delayed Int String ts : TrainStatus ts = Delayed 20 "The conductor took a short nap." 

which defines Delayed : Int -> String -> TrainStatus .

If you are given TrainStatus , you can extract Int and String from it using pattern matching:

 case ts of OnTime -> "Your train is on time!" Delayed minutes reason -> "Your train has been delayed by " ++ toString minutes ++ " because " ++ reason 
+9
source

Actually, yes, you can think of it as a payload that goes along with each branch.

 type Msg = OnFetchMails (WebData (List Mail)) | OnFetchSmss (WebData (List SMS)) 

Means that the value of type Msg can be either OnFetchMails , which will have some value of type WebData (List Mail) to go with it; or it can be OnFetchSmss with its accompanying WebData (List SMS) .

They are sometimes called tagged unions, because they are very similar to the C-style union construct containing the tag value, which indicates which option in the union is currently active (and in fact, many languages ​​with such a structure implement them in this way )

They can also be modeled as a series of subclasses of the abstract base type Msg , adding memory for their payloads in each subclass and requiring the provision of useful data as constructor parameters.

+7
source

All Articles