I think first class modules might be what I want, but I’m not sure. Here’s what I’m trying to do:
Let’s say I have some items which I want to add to a container but at runtime I want to decide whether items in the container should be unique. If true I want to use a Set if false I want to use a List.
In other languages this would be simple to do at runtime by creating different instances of an interface, what is the idiomatic way of doing this kind of think in OCaml?
Here’s a silly example of the kind of thing I’d like to be able to do:
let count_items (unique : bool) (items : int list) = let container = if unique then Thing.empty (module Set) else Thing.empty (module List) in let added = List.fold items ~init:container ~f:(fun c v -> Thing.add c v) in Thing.count added