Check out a free preview of the full Introduction to Elm, v2 course

The "Records" Lesson is part of the full, Introduction to Elm, v2 course featured in this preview video. Here's what you'd learn in this lesson:

Richard introduces interactions in Elm by first explaining what records are, and how to update them.

Preview
Close

Transcript from the "Records" Lesson

[00:00:00]
>> Richard Feldman: Let's talk about a few things here. First is records, then record updates, those go hand in hand. Booleans, those may be familiar. And finally, the Elm Architecture, which is what we're gonna use to describe interaction in our application. So let's start with records. [COUGH] This is a record in Elm, it's got a couple of fields, so name, x and y.

[00:00:19]
It looks a little bit similar in syntax to a JavaScript object, except that it's got equals instead of a colon. There are a couple of pretty substantial differences in terms of how records work, compared to JavaScript objects. But syntactically, they got a lot of similarities, so the equals being sort of the biggest difference.

[00:00:36]
In terms of how you read things off of record fields, that does look pretty much identical to how it does in JavaScript. So record.name will evaluate to foo, record.x will evaluate to 1, record.y will evaluate to 3. Some differences between Elm records and JavaScript objects, records do not have prototypes.

[00:00:54]
They also don't have this, and they also don't have any mutation. So they're sort of pretty bland in terms of feature set. Really all they do is hold on to data in an immutable way. So you can read stuff off of there, you can instantiate one, and that's pretty much all you can do with a record.

[00:01:11]
So basically records are sorta of plain immutable data. Okay, if you want to do records sorta of vertically like these you can say like a newRecord = [name = "bar", x = 1, y = 3]. So this is a record that is sorta of the same shape as the original record, but it has a different name value.

[00:01:33]
So the name field has been changed. You can use record update syntax like this to sort of do the same thing as what we did down below except with a little bit more conciseness to it. So here, since x and y were not changing, we didn't bother specifying them again like we did down here.

[00:01:51]
We just said record pipe name = "bar", and that basically means, I wanna make a new version of this record that is exactly the same as the old one, except that this one field has changed. If you want, you can change multiple fields at once. You could add a comma here and say x = 7 and change name and x.

[00:02:10]
But essentially, these are doing the same things. Since records are immutable, when we say update, what we really mean is give me a new record that's also immutable, it's just based on the old one. We're using the old one as a starting point and then only changing a couple of things like maybe one thing about it.

[00:02:26]
Questions on records and record updates?

Learn Straight from the Experts Who Shape the Modern Web

  • In-depth Courses
  • Industry Leading Experts
  • Learning Paths
  • Live Interactive Workshops
Get Unlimited Access Now