Join egghead, unlock knowledge.

Want more egghead?

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

Unlock This Lesson

Already subscribed? Sign In


    Create and Access Records in Elm

    Enrico BuonannoEnrico Buonanno

    Records in Elm are very similar to Objects in Javascript. They are useful to represent domain entities and other program data.

    In this lesson we'll go over how to create Records, use them in functions, and how to access data from them. Try it out for yourself here in the Elm repl!



    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 Records in elm are somewhat similar to objects in JavaScript. Let's start up the elm repo.

    00:08 Let's create a record. Let me call it Joe, and use curly braces. Inside the curly braces, you put a list of field names. For example, we could say that Joe has a name, and his name is Joe. We could say he has an age, and his age is 21.

    00:28 Now, you can see the string representation of the value you've created. To the right of the colon is the type. It represents that this is a record type with the field called age, whose type is number, and a field called name with type string.

    00:46 Let me also create another record. I'll call him Bill. The name will be Bill, and age will be 22. Essentially, records are just a way to associate names with fields. In terms of the data, there is nothing different between tuples and records.

    01:04 You could say that the tuple Joe and 21 really represents the same data as this value Joe. Records however, allow you to associate names with different fields. For this reason, they're a lot more readable. You can say to retrieve the name of the record Joe, and naturally that's Joe. Likewise, you could say Bill.age, and you get Bill's age.

    01:32 When you use the dot syntax, what you are actually using is an accessor. For example, if I just type .name, it indecipherable this is a function that takes record that must have a field called name of type A, and it returns that field. For example, you could use the .name field accessor, and apply it to the Joe record. Naturally, that would be the exactly the same as typing

    02:02 This can be quite useful, if you have, for example, a list. Let me create a list that contains both Joe and Bill. Now you could use map, and map.age function to the list. You get a list with the age field extracted from each record in L1. Notice that, although we are attaching semantic meaning to these records, we haven't really defined a person type as such.

    02:32 Records are really just data, where we associate names to each piece of data. If you look at the .name accessor again, notice that, it only requires that the given record have a name. For example, let's say that we have a dog now. Let's call him Fido. He'd have a name of Fido. He'd also have a breed, and let's say that he's a husky.

    02:58 Notice that, .name can also be given a dog just as well as a person. Just to make this point again, let me write a function greet. Greet takes a record R, and returns to string, "Hello" concatenated with the name of "R". If you look at the signature of this function, it returns a string. It takes an input that must be record with the name field.

    03:28 As a result, we can greet a person, greet(Joe). We can also greet a dog, so greet(Fido) that says, "Hello Fido." Greet, as a signature says, really can take any record, as long as it has a field called name of type string.