Introduction to Elm, v2 Records
Transcript from the "Records" Lesson
>> 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: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?