Try to further understanding the interface/module of OCaml

心已入冬 提交于 2019-12-02 08:42:56

Here's an example that shows what I think you're asking for:

# module type HASH = sig type t val hash : t -> int end ;;
module type HASH = sig type t val hash : t -> int end
# module I = struct type t = int let hash i = i end ;;
module I : sig type t = int val hash : 'a -> 'a end
# module J = struct type t = int end ;;
module J : sig type t = int end
# module M : HASH = I ;;
module M : HASH
# module N : HASH = J ;;
Error: Signature mismatch:
       Modules do not match: sig type t = int end is not included in HASH
       The field `hash' is required but not provided

The extra ": HASH" specifies that the module must match the HASH signature (and it also restricts it to that signature).

Just as a side comment, I believe the OCaml module system is world famous for its expressivity (at least in module system circles). I'm still a beginner at it, but it is worth studying.

Since 3.12.1 OCaml allows this syntax for opening and aliasing modules:

let foo .... =
  let module HashTable = HashMap in (* magic is here *)
  let h = HashTable.create () in
  ....

So u just need to rename module what you are using where you are using it.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!