Syntax Error Compiling Code with Functor Module

For the following code, when I tried to compile I get this error but I don’t know why:
----- error message -----
File “test.ml”, line 20, characters 2-8:
20 | module P = Csv(ShowInt)
^^^^^^
Error: Syntax error
----- end of error message -----

----- Code (test.ml) -------

module type SHOW = sig
type t
val show : t -> string
end

module ShowInt = struct
type t = int
let show = string_of_int
end

module Csv(S:SHOW) = struct
let rec to_csv : S.t list -> string =
function
| [] -> “”
| [x] -> S.show x
| h::t -> S.show h ^ “,” ^ to_csv t
end

let () =
module P = Csv(ShowInt)
P.to_csv [1;2;3]

1 Like

It is incorrect to do module P = Csv(ShowInt) inside an expression. Module definitions are top-level declarations.

You can define modules locally with let module M = rhs in expr:

let () =
  let module P = Csv(ShowInt) in
  P.to_csv [1;2;3]

However, now there is a type error because P.to_csv [1;2;3] does not have type unit.

3 Likes

OK. I know what I did wrong now! Thank you!