I confused with module type in Ocaml.
I wondered that in which situation we should use module type?
I usually use module sig in .mli to expose some detail, and put corresponding implementation module struct in .ml.
For example:
.mli
module A:
sig
type t = T of string
end
.ml
module A =
struct
type t = T of string
end
For this reason, I think Ocaml's module like .h and .c file in C.
I know that module type can declare a interface, but the interface were not as same as Java's interface.
Like an example in book:
open Core.Std
module type ID = sig
type t
val of_string : string -> t
val to_string : t -> string
end
module String_id = struct
type t = string
let of_string x = x
let to_string x = x
end
module Username : ID = String_id
module Hostname : ID = String_id
type session_info = { user: Username.t;
host: Hostname.t;
when_started: Time.t;
}
let sessions_have_same_user s1 s2 =
s1.user = s2.host
The preceding code has a bug: it compares the username in one session to the host in the other session, when it should be comparing the usernames in both cases.
It seems that module type can not provide a new common super type for it's implementation.
What the real application for module type??