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


    Define Extensible Records in Elm

    Enrico BuonannoEnrico Buonanno

    Extensible records in Elm are records that contain a specified set of fields, but may additionally contain any other set of fields.



    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


    Instructor: 00:01 Here, I've defined a type alias, person, and a type alias, dog. I have value Joe of type person and the value Fido of type dog.

    00:10 One thing that we saw before is that if I define a greet function that uses the name of some given record, then I can use this function with a person, Joe. You see that the output is "Hello Joe." I could also use it with a dog. Let's greet Fido. You can see this output's "Hello, Fido." Greet doesn't really care what type of records it's given, as long as that record has a name field.

    00:38 What is the type of greet, exactly? Let's get the compiler to help us out. Let me write a signature that is obviously wrong. Let me say that greet is an int. The compiler gives us a very useful message. It says, "You're saying it's an int, but I actually think it's a function." That's the error here. It takes a record of this type and returns a string.

    01:00 Let me copy this signature and let me paste it here. Indeed, now it compiles. What this notation means is a record that may have some properties, that's the a parameter, but must have a name of type string. The compiler can infer this because I'm using the name element and concatenating it with a string, so it must be a string.

    01:23 This sort of notation in a type signature is not so clear, especially if your function relies on multiple fields. We can put this into type alias as well, which we could call named. This needs a type parameter, which I've called r. I could say type alias named r. Let me paste the definition I've copied before. I'm using r as the type parameter. Let me try to compile this, and this works.

    01:51 Now, I have much clearer signature that says that greet takes a named thing and returns a string. These sort of records, like named, are called extensible records. Whereas, a normal record, like a person, lists a set of fields, an extensible record also lists a set of fields, but it's saying you can have any other fields, as well.

    02:13 You can write functions to work with these extensible records, such as greet. These functions can not only get the value of these fields like we're doing now, but they can also set the values. For example, let me write the function rename that takes a named record and returns another value of the same type where the name will be updated.

    02:35 Implementation will be quite simply, r, with the name property set to name. This name must actually be given as an argument, so a new name. Let's call it newname for clarity. It must be given, and it's a string.

    02:54 For example, if I take Fido and I rename him to Jolly, and I then create a string representation, you see that now we've updated the name to Jolly but we still have the previous value of the dog, such as his breed. We could similarly rename Joe to John. You see that the name has been updated to John, but the occupation is Joe's previous occupation.