Try to further understanding the interface/module of OCaml

前端 未结 3 1698
眼角桃花
眼角桃花 2021-01-28 19:33

I understand in OCaml there are concepts of interfaces and module.

And I understand how to use them now.

However, what I don\'t under

3条回答
  •  -上瘾入骨i
    2021-01-28 20:29

    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.

提交回复
热议问题