2016-08-03 58 views
1
module type FOOable = sig 
    type 'a t 
    val foo : 'a -> 'a t 
end 

module type FOO_FUCNTOR = 
    functor (Elt : FOOable) -> 
    sig 
     type 'a t 
     val foo_alias : 'a -> 'a t 
     (* ... *) 
    end 

我怎么能是指由FOOable定义的类型'a t,因为它是不可能使用Elt.t?因此,在这个例子中OCaml的函子和类型问题

它会成为type 'a t = 'a list

module MakeFoo : FOO_FUNCTOR = 
    functor (Elt : FOOable) -> 
    struct 
    type 'a t = ??? (* I want the type 'a t belonging to Elt *) 
    let foo_alias = Elt.foo 
    end 

module FooableList = struct 
    type = 'a list 
    let foo x = [x] 
end 

module FooList = MakeFoo(FooableList) 

let a = FooList.foo_alias 2 

回答

4

您可以简单地输入'a Elt.t,你一定会参照正确的类型。

module MakeFoo : FOO_FUNCTOR = 
    functor (Elt : FOOable) -> 
    struct 
    type 'a t = 'a Elt.t 
    let foo_alias = Elt.foo 
    end 

注意,如在FOO_FUNCTOR的定义,类型平等是隐藏的,'a t'a Elt.t之间的联系不会是MakeFOO定义之外可见的(但可能是你在找什么)。