Two newbie questions about module

Hello, I am learning how module works in OCaml.

(Edit: sorry guys, I figured what went wrong with my first code example, the code below is working now)

First question, I don’t quite get why the example code below is not working:

file: lib/s.ml

module type S = sig
  type error
  type t
  val get : string -> (t option, error) result Lwt.t
end;;

module Q = struct
  type error = Some_error of string
  type t = { id : int64 }

  let get s =
    match s with
    | "ok" -> Lwt.return (Ok (Some { id = 1L }))
    | _ -> Lwt.return (Error (Some_error "error"))
end

module Q_s : S = Q

The error message I am getting:

File "lib/s.ml", line 17, characters 17-18:
Error: Signature mismatch:
       ...
       Values do not match:
         val get : string -> (int option, error) result Lwt.t
       is not included in
         val get : string -> (t option, error) result Lwt.t
       File "lib/s.ml", line 4, characters 2-52: Expected declaration
       File "lib/s.ml", line 11, characters 6-9: Actual declaration

I can’t seem to make t concreate (e.g. a record)…

Second question, the above code snippet e.g. is in lib/s.ml, suppose I create another file lib/s.mli with

module type S = sig
  type error
  type t
  val get : string -> (t option, error) result Lwt.t
end;;

And I have to include the above piece of code in lib/s.ml as well which is a bit redundant, I thought it might be sufficient to have the module type S just in mli file, apparently the compiler would complain Unbound module S

For the second question, the Jane Street way of solving this (used in Base, Core, etc) is to write a third file lib/s_intf.ml containing the module type declaration, and include that in both lib/s.ml and lib/s.mli.

1 Like

Thanks @smolkaj, this is really good to know.