Can you identify the problem here?
Why are the type constraints not working?
1. $ ocamlopt test_type.mli test.ml
File "test.ml", line 4, characters 0-186:
Signature mismatch:
Modules do not match:
sig type t = P.t val init : 'a -> 'b -> 'a * 'b end
is not included in
Test_type.BaseType
Values do not match:
val init : 'a -> 'b -> 'a * 'b
is not included in
val init : string -> int -> t
2. init in MakeBase() commented out and second version of init
uncommented (i.e. explicit types are given for everything)
$ ocamlopt test_type.mli test.ml
File "test.ml", line 9, characters 43-54:
This expression has type string * int but is here used with type t = P.t
----- test_type.mli attached here ---------
module type ParamType =
sig
type t
val is_true: t -> bool
end
module type BaseType =
sig
type t
val init: string -> int -> t
end
module MakeBase(P : ParamType) : BaseType with type t = P.t
----- end of test_type.mli ---------------
------- test.ml begins -------------
open Test_type
module MakeBase(P : ParamType) : BaseType =
struct
type t = P.t
let init text len = (text, len)
(* let init (text: string) (len:int) : t = (text, len) *)
(* the second definition with explicit types fails also *)
end
module Param : ParamType =
struct
type t = string * int
let is_true arg = let (_, len) = arg in len > 0
end
module MyModule = MakeBase(Param)
let _ =
let (result, len) = MyModule.init "text" 10 in
Printf.printf "%s %dn" result len
------- end of test.ml --------
.