有什么方法可以在不使用模块功能的情况下打印模块类型?

时间:2020-10-24 02:34:53

标签: io ocaml

假设我有一个像这样的模块:

module type MyModule = 
   sig
      type t
      func1 : ... -> t
   end

(* Implementation *)
module SomeModule : MyModule = 
   struct
      type t = int
      let func1 ... : t =
         (* do something and return type t *)
      end

现在,我在模块外部的某个地方调用了func1,并获得了类型为MyModule.t的值:

let x = SomeModule.func1 ... in
   print_int x   (* Which doesn't works *)

所以我想知道有什么方法可以打印x吗? 感谢您的回答!

1 个答案:

答案 0 :(得分:0)

简短的回答是“否”。您已经专门将类型t设为抽象,因此无法对t之外的类型SomeModule进行任何操作。

当然有一些偷偷摸摸的方法来破坏OCaml中的类型系统(就像每种语言一样),但是通常来说,您应该避免像瘟疫一样避免它们。