我想知道 OCaml 是否可以执行类似于以下代码(在 TypeScript 中)的操作:
interface User {
name: string
email: string
}
function titi(user: User) {
console.log(user)
}
let u1 = {name: "v2"}
let u2 = {...u1, email: "<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="90e6a2d0e8bee9beea" rel="noreferrer noopener nofollow">[email protected]</a>"}
titi(u2)
最佳答案
目前尚不完全清楚您的要求。但 OCaml 中最接近的构造可能是记录构造函数中的 with
:
type user = { name : string; email : string }
let user_a = { name = "ua"; email = "e" }
let user_b = { user_a with name = "ub" }
(当然,在 OCaml 中,您必须构造完整的记录值,不能只有其中一个字段。)
关于ocaml - OCaml 的记录是否有类似于 JavaScript 的扩展运算符?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/74745738/