Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Updating a record in Elm

Tags:

elm

NOTE: This is my first time ever looking at Elm and I just learned about its existence last week by accident.

When you update a record, are you really updating a record or just creating a new one.

> { bill | name = "Nye" }
{ age = 57, name = "Nye" }

> { bill | age = 22 }
{ age = 22, name = "Gates" }

I would expect:

> { age = 22, name = "Nye" }

Since there were two updates done on 'bill'.

Reading from the Elm language site, I know there aren't destructive updates. A new object (with the same name?) is created and shares the fields that weren't changed of the old(er) object. But from these examples, it doesn't seem like 'bill' is being updated at all. It looks more like 'bill' is being copied, that copy is being updated, and a new record called 'anonymous Will' is being created. A completely new record.

So what am I misunderstanding here?

like image 694
On The Net Again Avatar asked Dec 06 '25 07:12

On The Net Again


1 Answers

It looks like you're working in the Elm REPL? Doesn't look like you're assigning the output of your first update to anything. This means when you do your second update on age, you're still just making a copy of the first object, which has the same name, rather than the second object you named Nye.

-- Create Bill Gates
billGates = { age = 100, name = "gates" }

-- Copy to Bill Nye
billNye = { bill | name = "Nye" }

-- Copy to a younger Bill Nye
youngBillNye = { billNye | age = 22 }

Make sense?

like image 112
timothyclifford Avatar answered Dec 10 '25 11:12

timothyclifford



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!