问题描述
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吗? 感谢您的回答!
解决方法
简短的回答是“否”。您已经专门将类型t
设为抽象,因此无法对t
之外的类型SomeModule
进行任何操作。
当然有一些偷偷摸摸的方法来破坏OCaml中的类型系统(就像每种语言一样),但是通常来说,您应该避免像瘟疫一样避免它们。