Can I inherit from an OCaml class chosen at runtime?
ocaml
Solution
Not contrary to what Jeffrey said, but you can achieve this using first-class modules. Also, I'm not sure that you really need to create classes on runtime, maybe creating object would be enough. If what you want to get is different behavior, then this will be enough.
Problem
So right now I have two classes of the same class type, e.g. ``` class foo : foo_type = object ... end class bar : foo_type = object ... end ``` I'd like to have a third class that inherits from either `foo` or `bar` at runtime. E.g. (pseudo-syntax) ``` class baz (some_parent_class : foo_type) = object inherit some_parent_class ... end ``` Is this possible in OCaml? Use case: I'm using objects for constructing AST visitors, and I'd like to be able to combine these visitors based on a set of runtime criteria so that they only do one combined traversal over the AST. Edit: I think I have 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 a little roundabout to have to wrap a class in a module in order to make it "first-class". If there's a more straightforward way please let me know.