So now I have two classes of the same type of class, for example
class foo : foo_type = object ... end class bar : foo_type = object ... end
I would like to have a third class that inherits from foo or bar at runtime. For instance. (Pseudo-syntax)
class baz (some_parent_class : foo_type) = object inherit some_parent_class ... end
Is this possible in OCaml?
Use case: I use objects to build AST visitors, and I would like to be able to combine these visitors based on a set of runtime criteria so that they perform only one combined AST tour.
Edit: I think I found a way to create the desired class using first-class modules:
class type visitor_type = object end module type VMod = sig class visitor : visitor_type end let mk_visitor m = let module M = (val m : VMod) in (module struct class visitor = object inherit M.visitor end end : VMod)
However, it seems to twirl a bit to wrap a class in a module to make it โfirst-classโ. If there is an easier way, let me know.
source share