Join egghead, unlock knowledge.

Want more egghead? It's 45% off for a limited time only!

This lesson is for members. Join us? Get access to all 3,000+ tutorials + a community with expert developers around the world.

Unlock All Content for 45% Off

Already subscribed? Sign In

Save 45% for a limited time.

Get access to all courses and lessons on egghead today.


    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.



    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


    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.