2016-06-05 16 views
4

Ich habeWie umfasst die Arbeit?

module type T = sig 
    type t 
end 

und

module Make (TypeProvider : T) = struct 
    include TypeProvider 
    type d = Wrapped of t 
end 

und

module Test = struct 
    include Make (struct type t = ForWrap end) 
    let f = function | Wrapped ForWrap ->() 
end 

Ich stellte mir vor-Test nach der Kompilierung wie

module Test = struct 
    type t = ForWrap 
    type d = Wrapped of t 
    let f = function | Wrapped ForWrap ->() 
end 

Aber in real, es ist nicht übersetzbar c Ode. OCaml sagt mir:

module Test = struct 
    include Make (struct type t = ForWrap end) 
    let f = function | Wrapped ForWrap ->() 
           ^^^^^^^ 

Error: Unbound constructor ForWrap

end 

Und ich kann nicht verstehen, warum. Was ist das Problem in meiner Lösung?

+0

Funktor-Anwendung ist eine Laufzeitoperation, und Sie können '' include'' nicht als reinen syntaktischen Ebenenersatz behandeln – objmagic

Antwort

6

Lassen Sie uns die Unterschrift von Make (struct type t = ForWrapp end) sehen:

module M = Make(struct type t = ForWrapp end) 

ocamlc -c -i xxx.ml zeigt Ihnen die Signatur dieses Moduls:

module M : sig 
    type t 
    type d = Wrrapped of t 
end 

Beachten Sie, dass der Konstruktor ForWrapp im Ergebnis Modul nicht verfügbar ist. Aus diesem Grund wird der Code nicht überprüft.

Warum ist der Konstruktor weg? Dies ist, da die Argumentsignatur des Funktors MakeT ist. T definiert einen Typ t, der abstrakt ist. Selbst wenn Sie Make auf ein Modul mit detaillierterer Signatur (hier struct type t = ForWrapp end) anwenden, wird es auf T gezwungen und die Konstruktorinformationen gehen verloren.

Verwandte Themen