Become a member
to unlock all features

Level Up!

Access all courses & lessons on egghead today and lock-in your price for life.


    Records in Elm


    Records are important data structures in Elm. In this lesson we learn to create a record as well as describe the record by creating a type. We will also see how these new types can be used to clean up our annotations.



    Become a Member to view code

    You must be a Pro 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
    orLog In




    Records are data structures in Elm. Let's create a record named Resources. This will contain resources that we want to store for learning Elm. The record structure is wrapped in curly braces. We will create a key value pair named Name, and set it equal to a string.

    A record can also hold different types. We can add isComplete as a bool and an ID as an int.

    Let's do a little set up to make this app run. We already importing HTML, but let's bring in everything with the double dots. In Main, let's reference a view function we are going to write, and pass in resources. Finally, create a view, and it expects resources and writes out name.

    We can access values from a record by using it's name and a dot and then then value. We will use forward function to pass it into text.

    Using square brackets, we can make a list of our records. While we're at it, let's add a link key value.

    Now that we have a list of items, we will need to map over them to transform our list into HTML.

    If we wanted, we could break up the view function by creating a function that handles the mapping of the list of resources.

    Right now, if we wanted to add a type annotation to resource list, it would look like this.

    Yikes. This is the shape of our data, but it's not very readable. We can break that out and create a resource type.

    This looks very similar to our actual record, but now we use semicolons. Use the keyword type, then alias, then the name of our new type.

    Alias says anytime someone refers to the capital R Resource, this is the structure of the data. Now we can use the name Resource in our type annotation.

    This says we are expecting a list of Resource. We could also create a type for that.

    Let's update our annotation again and add one for view. Let's verify that works.

    Being able to give a name to the structure of our data really cleans up our type annotations, and we now have the basics of records.