user6307701
user6307701

Reputation:

Elm recursive type definition

Just taking a peek at elm code and came across the following type definition:

type Cmd msg = Cmd

I just can't seem to understand how this works. Anybody can explain?

Upvotes: 4

Views: 511

Answers (1)

Art Yerkes
Art Yerkes

Reputation: 1284

That isn't a recursive type definition, it's defining a type Cmd as a union type with one label, Cmd, which contains no extra information. Usually definitions like this are intended to signal that the type represents values that have no individual meaning in elm.

In this case, the type is defined that way because all operations on Cmd are hidden in platform code, so there's no need for users to be able to examine or destructure Cmd values. Cmd needs to expose a type variable in order to preserve the type safety of Cmd values, because they encapsulate a promise to yield a message of a given type and that type can be be changed via Cmd.map .

Upvotes: 9

Related Questions