在 OCaml 中复制字段

发布于 2024-09-29 00:04:25 字数 468 浏览 1 评论 0原文

我有一个关于 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=3b=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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

污味仙女 2024-10-06 00:04:25

是的,这就是确切的语法。

let y = {x with c=4}

yeah, and that's the exact syntax.

let y = {x with c=4}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文