在 OCaml 中复制字段
我有一个关于 OCaml 记录的非常基本的问题。假设我定义了一条记录:
type r = {a: int; b: int; c: int}
let x = {a=3; b=8; c=2}
现在,假设我想创建一个新记录,该记录的所有字段都等于x
,但有c=4
。我可以写:
let y = {a=3; b=8; c=4}
但这很烦人,因为不需要重写 a=3
和 b=8
。我也可以写:
let y = {a=x.a; b=x.b; c=4}
但如果记录有很多字段,这仍然不好。有没有办法写类似:
let y = {x with c=4}
或类似的东西?
I have a very basic question regarding OCaml records. Suppose I have a record defined:
type r = {a: int; b: int; c: int}
let x = {a=3; b=8; c=2}
Now, suppose I want to create a new record which has all fields equal to x
but which has c=4
. I could write:
let y = {a=3; b=8; c=4}
but this is annoying because there's not need to re-write a=3
and b=8
. I could also write:
let y = {a=x.a; b=x.b; c=4}
but this is still not good if the record has many fields. Is there any way of writing something like:
let y = {x with c=4}
or something of the sort?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
是的,这就是确切的语法。
yeah, and that's the exact syntax.