Enter Your Email Address to Watch This Lesson

Your link to unlock this lesson will be sent to this email address.

Unlock this lesson and all 830 of the free egghead.io lessons, plus get Elm content delivered directly to your inbox!



Existing egghead members will not see this. Sign in.

Just one more step!

Check your inbox for an email from us and click link to unlock your lesson.



Records in Elm

3:28 Elm lesson by

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.


egghead.io comment guidelines

Avatar
egghead.io

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.

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.

HEY, QUICK QUESTION!
Joel's Head
Why are we asking?