Hacker News new | ask | show | jobs
by dan-robertson 2050 days ago
Module signatures may contain types.

So you can write a signature like:

  module type VectorSpace = sig
    module Field : Field
    type t
    val zero : t
    val (+) : t * t -> t
    val (*) : Field.t * t -> t
  end
In OOP it becomes hard to write an interface like this simple example, and more complex examples become harder still.
1 comments

Can you give an example of the value this would provide that isn't provided by generics?
One thing the example shows is the classic problem with OO's mechanism, namely binary functions. For a given class, something like `+` is impossible to implement elegantly, it has to bias one operand over the other -- after any syntactic sugar, it's always `arg1.op(arg2)`. Module functors resemble true ADTs more, in that they do not dispatch from a live instance of the datatype.