2 ms·
Can you provide an example of how functors make Ocaml code different from F# code in practice?
by cwyers 11y ago
Can you provide an example of how functors make Ocaml code different from F# code in practice?
- tel 11y agoSure. I'm not super familiar with F#, but I can state things which depend upon functors. A good example is the Set.Make functor in the OCaml stdlib. Elements in a Set must be orderable for efficiency's sake, so to construct a Set you parameterize it over a module specifying not only the type but also its ordering. module type Set = sig module type OrderedType = sig type t val compare : t -> t -> int end module type S = sig type elt type t val empty : t (* ... *) end module Make : functor (Ord : OrderedType) -> S with type elt = Ord.t end Here the OrderedType module signature represents any type that also has an ordering. The S signature represents the result signature of calling the Make functor which is passed any OrderedType-substantiating module and uses it to construct the (Set.S with type elt = Ord.t) module.