I don’t get how to abstract module types;
Here’s what I tried - let’s say that we have some module with a function make
module type B = sig
type t
type err
type 'a do_things = t -> ('a, err) result
val make: string do_things
end
we can implement the types and everything works as expected - val make : string do_things
module My_b: B = struct
type t = int
type err = string
type 'a do_things = t -> ('a, err) result
let make x =
print_endline (string_of_int x);
Ok "ok"
end
but these 3 types - t
, err
and do_things
are used in multiple modules, so we want to define them in a share module type, instead of re-defining them
module type A = sig
type t
type err
type 'a do_things = t -> ('a, err) result
end
now we can define the same module, but with the types moved:
module type C = sig
module A: A
val make: string A.do_things
end
and as before - the implementation works
module My_c: C = struct
module A = struct
type t = int
type err = string
type 'a do_things = t -> ('a, err) result
end
let make x =
print_endline (string_of_int x);
Ok "ok"
end
but since we have a common implementation of A, we define it once and reuse it:
module My_a: A = struct
type t = int
type err = string
type 'a do_things = t -> ('a, err) result
end
let’s try the implementation again
module My_c2: C = struct
module A = My_a
let make x =
print_endline (string_of_int x);
Ok "ok"
end
error - signature mismatch, but I don’t understand - My_a
is the same as the defined types above?
Values do not match:
val make_something : int -> (string, 'a) result
is not included in
val make_something : string A.do_things
how to make this work? how can I define an abstract module type and reuse it?