在文档中: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
关于ocaml - 带有可选和可变字段的记录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51013072/