更新榆树中的记录 [英] Updating a record in Elm

查看:88
本文介绍了更新榆树中的记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

注意:这是我第一次看榆木,上周我才偶然得知它的存在。

当您更新记录,是您真的要更新记录还是只是创建新记录。

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" }

期望:

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

由于对 bill进行了两次更新。

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?

推荐答案

看起来您在Elm REPL工作吗?您似乎没有将第一次更新的输出分配给任何东西。这意味着当您进行年龄的第二次更新时,您仍然只是在复制具有相同名称的第一个对象,而不是复制名为Nye的第二个对象。

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 }

有道理吗?

这篇关于更新榆树中的记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆