This Lesson is for Members

Subscribe today and get access to all lessons! Plus direct HD download for offline use, enhances transcripts, member comment forums, and iTunes "podcast" RSS feed. Level up your skills now!

Unlock This Lesson

Already subscribed? Sign In

Autoplay

    Model Data with Elm's Recursive Union Types

    Enrico BuonannoEnrico Buonanno

    Recursive Union Types in Elm enable you to simply and effectively model list-like or tree-like structures.

    For an explanation of simple union types, watch Define simple union types in Elm.

    elmElm
    Code

    Code

    Become a Member to view code

    You must be a Member to view code

    Access all courses and lessons, track your progress, gain confidence and expertise.

    Become a Member
    and unlock code for this lesson
    Transcript

    Transcript

    00:00 Another interesting feature of union types is that they can have a recursive definition. Here, I still have some code from a previous video where we are creating some data of type Rating, and the Elm app just displays the data.

    00:14 Now, let me create a new union type, Breed, to represent the breed of a dog. If we want our data to be of type Breed, we could use one of these values. Now, imagine that we also want to cater for crossbreeds. We could add another case where the breed is a crossbreed between two breeds.

    00:42 We can now have data that is, let's say half Husky and half Poodle. This compiles as you would expect. This is recursive, so you could say that one parent is Husky, and another parent in turn is a crossbreed between, say, a Poodle and a Beagle.

    Discuss

    Discuss