2

我正在尝试使用相互递归模块定义一个变量,假设一个 Todo 可以有很多 Note 并且 Note 可以属于一个 Todo:

module Sig = {
  module type NoteSig = {type t;};
  module type TodoSig = {type t;};
};

/* same file */
module Same = {
  module rec Note: Sig.NoteSig = {
    type t = {todo: Todo.t};
  }
  and Todo: Sig.TodoSig = {
    type t = {
      text: string,
      notes: array(Note.t),
    };
  };
};

/* different files */
module A = {
  module Note = (T: Sig.TodoSig) => {
    type t = {todo: T.t};
  };
};

module B = {
  module Todo = (N: Sig.NoteSig) => {
    type t = {notes: array(N.t)};
  };
};

module C = {
  module rec NoteImpl: Sig.NoteSig = A.Note(TodoImpl)
  and TodoImpl: Sig.TodoSig = B.Todo(NoteImpl);
};

/* impl */
let todo1: Same.Todo.t = {notes: [||]};
let todo2: C.TodoImpl.t = {notes: [||]};

let todo3 = Same.Todo.{notes: [||]};
let todo4 = C.TodoImpl.{notes: [||]};

Js.log2(todo1, todo2);

但是我不能用这种类型定义一个变量,编译器告诉我:

36 │
37 │ /* impl */
38 │ let todo1: Same.Todo.t = {notes: [||]};
39 │ let todo2: C.TodoImpl.t = {notes: [||]};
40 │

The record field notes can't be found.

If it's defined in another module or file, bring it into scope by:
- Annotating it with said module name: let baby = {MyModule.age: 3}
- Or specifying its type: let baby: MyModule.person = {age: 3}

如果有帮助,请使用 Ocaml 中的相同代码:

module Sig =
  struct
    module type NoteSig  = sig type t end
    module type TodoSig  = sig type t end
  end
module Same =
  struct
    module rec Note:Sig.NoteSig = struct type t = {
                                           todo: Todo.t;} end
    and Todo:Sig.TodoSig =
      struct type t = {
               text: string;
               notes: Note.t array;} end
  end
module A =
  struct module Note(T:Sig.TodoSig) = struct type t = {
                                               todo: T.t;} end end
module B =
  struct
    module Todo(N:Sig.NoteSig) = struct type t = {
                                          notes: N.t array;} end
  end
module C =
  struct
    module rec NoteImpl:Sig.NoteSig = A.Note(TodoImpl)
    and TodoImpl:Sig.TodoSig = B.Todo(NoteImpl)
  end
let todo1: Same.Todo.t = { notes = [||] }
let todo2: C.TodoImpl.t = { notes = [||] }
let todo3 = let open Same.Todo in { notes = [||] }
let todo4 = let open C.TodoImpl in { notes = [||] }
let _ = Js.log2 todo1 todo2

抱歉,代码太长,请丢弃下面的这些行。

4

2 回答 2

2

首先,最简单的解决方案是使类型相互递归

type todo = { text:string, type todos:array(todo) }
and note = { todo:todo } 

如果你真的需要将这两种类型拆分到单独的模块中,那么它们确实需要递归模块。

在这种情况下,关键思想是签名代表模块内容的完整规范,换句话说,签名

module type T = { type t }

是实现黑盒类型的模块的规范,t仅此而已

因此,签名约束Note:Sig.NoteSigTodo:TodoSig

module rec Note:Sig.NoteSig = { type t = { todo: Todo.t} }
and Todo:Sig.TodoSig = {
  type t = { text: string, notes: array(Note.t)}
}

实际上正在删除有关 Note.t 和 Todo.t 实际实现的所有信息。

你想要的是先写完整的签名:

    module type NoteSig = { type todo; type t = {todo: todo} }
    module type TodoSig = {
      type note;
      type t = { text: string, notes: array(note)}
   } 

然后您可以将实现编写为

 module rec Note: NoteSig with type todo := Todo.t = { type t = { todo: Todo.t} }
 and Todo: TodoSig with type note := Note.t = 
 { type t = { text: string, notes: array(Note.t)} }

如果您的模块中只有类型,则可以使用以下版本

 module rec Note: NoteSig with type todo := Todo.t = Note
 and Todo: TodoSig with type note := Note.t = Todo

对于函子版本,如果您不需要模块中定义的函数,最简单的实现就是

module Make_Todo(Note: { type t;}) = {
  type t = { text:string, notes:array(Note.t) }
}
module Make_Note(Todo: { type t;}) = { type t = { todo:Todo.t} }

(作为一般规则,对于初学者来说,通常最好让类型检查器推断函子的结果类型。)然后你可以用它们来实例化它们

module rec Todo: TodoSig with type note := Note.t = Make_Todo(Note)
and Note : NoteSig with type todo := Todo.t = Make_Note(Todo)

如果您需要的不仅仅是 make 函子中其他模块的类型,您可以更进一步,指定函子的参数实现完整签名

module Make_Todo(Note: NoteSig) = {
  type t = { text:string, notes:array(Note.t) }
}
module Make_Note(Todo: TodoSig) = { type t = { todo:Todo.t} }

但是随后模块的实例化变得更加复杂

module rec Todo: TodoSig with type note := Note.t = 
  Make_Todo({ include(Note); type todo = Todo.t })
and Note : NoteSig with type todo := Todo.t = 
  Make_Note({ include(Todo); type note = Note.t }) 

并且遇到复杂错误的风险更大。

于 2018-07-05T12:25:39.580 回答
0

对于未来的访问者,如果两者都在同一个文件中,我会有答案:

module Same = {
  module rec Note: {
    type t = {
      title: string,
      todo: option(Todo.t),
    };
  } = Note
  and Todo: {
    type t = {
      title: string,
      notes: option(array(Note.t)),
    };
  } = Todo;
};

let todo: Same.Todo.t = {
  title: "helo",
  notes: Some([|Same.Note.{title: "one", todo: None}|]),
};

[@bs.module "util"] external inspect : ('a, 'b) => 'c = "inspect";
Js.log(inspect(todo, {"depth": 10}));

不过,我仍在寻找函子解决方案。

于 2018-07-05T10:21:18.653 回答