Qua*_* Le 4 ocaml reason bucklescript
假设我有一个1 -> n关系:一个待办事项可以有很多(或零)个笔记,而一个笔记可以有零个或一个待办事项。我怎样才能在 ReasonML 中实现这种关系?(绑定外部库)
这是我目前所带来的(这当然不起作用)
module Note = {
module Attributes = {
[@bs.deriving abstract]
type t = {
[@bs.optional]
id: float,
[@bs.optional]
text: string,
[@bs.optional]
todo: Todo.Attributes.t,
};
};
};
module Todo = {
[@bs.deriving abstract]
type t = {
[@bs.optional]
id: float,
[@bs.optional]
title: string,
[@bs.optional]
completed: bool,
[@bs.optional]
notes: array(Note.Attributes.t),
};
};
let todo = Todo.Attribute.t(~title="hello");
Run Code Online (Sandbox Code Playgroud)
如果 Note 和 Todo 在一个文件中,并且在单独的文件中怎么办?
我不熟悉 Reason,但在 OCaml 中,人们会使用相互递归模块来做这种事情,如下面的最小示例所示。
使用相互递归需要定义它们的模块类型:
module type NoteSig = sig
type t
end
module type TodoSig = sig
type t
end
Run Code Online (Sandbox Code Playgroud)
和实际模块:
module rec Note : NoteSig = struct
type t = {
todo: Todo.t
}
end
and Todo : TodoSig = struct
type t = {
notes: Note.t array
}
end
Run Code Online (Sandbox Code Playgroud)
如果您希望两个模块都在一个单独的文件中,您可以使用函子做几乎相同的事情(仍然使用模块签名,让我们在文件sig.ml 中说):
一毫升:
module Note (T:Sig.TodoSig) = struct
type t = {
todo: T.t
}
end
Run Code Online (Sandbox Code Playgroud)
b.ml:
module Todo (N:Sig.NoteSig) = struct
type t = {
notes: N.t array
}
end
Run Code Online (Sandbox Code Playgroud)
您现在可以在另一个文件中实例化您的模块:
毫升:
module rec NoteImpl = (A.Note(TodoImpl):NoteSig)
and TodoImpl = (B.Todo(NoteImpl):TodoSig)
Run Code Online (Sandbox Code Playgroud)
我只能假设有一种方法可以在 Reason 中做同样的事情,可能是在各处添加大量括号。希望能帮助到你。