3 ms·
Module signatures may contain types. So you can write a signature like: module type VectorSpace = sig module Field : Field type t val zero : t
by dan-robertson 6y 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.
- horsawlarway 6y agoCan you give an example of the value this would provide that isn't provided by generics?
- spopejoy 6y agoOne 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.