2016-08-03 4 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 

Comment puis-je me réfère au type 'a t défini par FOOable car il est impossible d'utiliser Elt.t?foncteurs OCaml et les questions de type

Ainsi, dans cet exemple, il deviendrait 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 

Répondre

4

Vous pouvez simplement taper 'a Elt.t et vous serez sûr de renvoyer le bon type.

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

Notez que comme dans la définition de FOO_FUNCTOR, l'égalité de type est caché, le lien entre 'a t et 'a Elt.t ne sera pas visible en dehors de la définition MakeFOO (mais peut-être ce que vous cherchez).