使用可选和可变字段记录

Record with optional and mutable fields

在文档中:https://bucklescript.github.io/docs/en/object.html 有包含可变字段和可选字段的记录示例。当我尝试同时使用两者时,它失败了:

编译:

type person = {
  mutable age: int;
  job: string;
} [@@bs.deriving abstract]

let joe = person ~age:20 ~job:"teacher"
let () = ageSet joe 21

添加 [@bs.optional] 属性:

type person = {
  mutable age: int;
  job: string [@bs.optional];
} [@@bs.deriving abstract]

let joe = person ~age:20 ~job:"teacher"
let () = ageSet joe 21

错误信息:

Line 7, 20: This expression has type unit -> person but an expression was expected of type person

第 7 行是 ageSet 行。

我在这里遗漏了什么吗?

我重新阅读了 documentation,这是我错过的部分

Note: now that your creation function contains optional fields, we mandate an unlabeled () at the end to indicate that you've finished applying the function.

type person = {
  mutable age: int;
  job: string [@bs.optional];
} [@@bs.deriving abstract]

let joe = person ~age:20 ~job:"teacher" ()
let () = ageSet joe 21