DEV Community

Eke Enyinnaya Diala
Eke Enyinnaya Diala

Posted on

8 3

Day 3 of Elm- Records

Records

According to Richard Feldman,

Records are plain, immutable data.

They are syntactically similar to Javascript objects but not exactly the same. They have no prototype and no this for example. What I really want to talk about is record updates.

Record update simply refers to how we mutate the properties of a record in Elm. The term "mutate" here is sort of a misnomer. A record update does not mutate the old record, they instead create new records based off of the old record. For example, we have this bit of elm code:

record = { a = 1, b = 2 }

newRecord = { record | a = 5 }

Enter fullscreen mode Exit fullscreen mode

The code above does not mutate record. It looks a lot like the . operator for mutating objects in Javascript but it is not the same. This is a lot closer to spreading record into newRecord and updating the fields you need. The way I think about this behaviour is along these lines in Javascript:

const record = Object.freeze({ a: 1, b: 2 });
const newRecord = Object.freeze({ ...record, a: 5 });

Enter fullscreen mode Exit fullscreen mode

We are using Object.freeze here because we need the objects returned in both cases to be immutable. You see that in both cases, no actual mutation does happen. This is good because it saves us a lot of trouble with side effects resulting from passing values by reference.

This is very very interesting!

Sentry blog image

How I fixed 20 seconds of lag for every user in just 20 minutes.

Our AI agent was running 10-20 seconds slower than it should, impacting both our own developers and our early adopters. See how I used Sentry Profiling to fix it in record time.

Read more

Top comments (1)

Collapse
 
rolograaf profile image
Lourens

What is fascinating to me is how the compiler finds a way to store this seemingly wasteful use of data in a very compact and efficent way, since it will only need to remember the difference between newRecord and record

Hostinger image

Get n8n VPS hosting 3x cheaper than a cloud solution

Get fast, easy, secure n8n VPS hosting from $4.99/mo at Hostinger. Automate any workflow using a pre-installed n8n application and no-code customization.

Start now

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay