OCaml functors :: counter-intuitive behaviour

前端 未结 4 429
[愿得一人]
[愿得一人] 2021-01-11 11:18

I am experimenting with the module language of OCaml (3.12.1), defining functors and signatures for modules and so on, mostly following the examples from Chapter 2 of the OC

4条回答
  •  北荒
    北荒 (楼主)
    2021-01-11 12:13

    Your key error was here:

    module IntType : SOMETYPE = struct type t end ;;

    When you ascribe the signature SOMETYPE, it's an opaque ascription, and the identity with int is lost. Type IntType.t is now an abstract type.

    You need instead to ascribe the signature SOMETYPE with type t = int.

    This transcript shows the difference:

    # module type SOMETYPE = sig type t end;;
    module type SOMETYPE = sig type t end
    # module IntType : SOMETYPE with type t = int = struct type t = int end;; 
    module IntType : sig type t = int end
    # module AbsType : SOMETYPE = struct type t = int end;;                 
    module AbsType : SOMETYPE
    

    The language-design issues around modules and ascription are well covered in the lead designer's 1994 paper on modules, types, and separate compilation. The hairy math parts can all be skipped.

提交回复
热议问题