Question

I'm a bit confused by the fact that (apparently) functor's signature in OCaml can be defined in two (seemingly) completely different ways. E.g. in the .mli file I can write:

module type A = sig
    type a
end

module type SigA = sig
    type t
end

module Make (MA : SigA) :
    A with type a := MA.t

module type Make2 = functor (MA : SigA) -> A with type a := MA.t

As far as I understand, in the example above Make and Make2 are entirely identical signature, yet the syntax looks quite radically different. Am I missing something? Is there any difference?

So is there a reason for two separate syntactic constructs? Historical reasons? IMO it is a bad practice to have two separate syntactic constructs serving the same purpose.

Was it helpful?

Solution

This is syntactic sugar, similar to that for functions (ie, let f x y = ... is shorthand for let f = fun x -> fun y -> ...). The motivation is presumably that in the long form multiple argument functors become quite hard to read:

module type A = sig end
module type B = sig end
module type C = sig end

module Foo (A:A) (B:B) (C:C) = struct
end

module Foo2 = functor (A:A) -> functor (B:B) -> functor (C:C) -> struct
end
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top