2016-08-03 38 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定義之外可見的(但可能是你在找什麼)。