ocaml - 带有可选和可变字段的记录

标签 ocaml ffi bucklescript

在文档中: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/

相关文章:

ocaml - 整数类型的模型 n

c++ - 从 C++ 代码调用 Haskell

javascript - 将记录列表导出到不带标签的 JavaScript 对象数组

python - OCaml 相当于 Python 生成器

ruby-on-rails - 支持复杂嵌套形式的框架 ala Rails 的 accepts_nested_attributes_for?

ocaml - 错误: Reference to undefined global `Num'

rust - 为什么对我的 FFI 函数的第二次调用无法匹配字符串比较?

c++ - haskell FFI : Interfacing with simple C++?

javascript - 如何在 ReasonML 中进行 protobuf?

dom - 从 DOM 中选择元素 - Reasonml/BuckleScript querySelector