title: Can I inherit from an OCaml class chosen at runtime?
body: <p>So right now I have two classes of the same class type, e.g.</p>
  
  <pre><code>class foo : foo_type = object ... end
  
  class bar : foo_type = object ... end
  </code></pre>
  
  <p>I'd like to have a third class that inherits from either <code>foo</code> or <code>bar</code> at runtime. E.g. (pseudo-syntax)</p>
  
  <pre><code>class baz (some_parent_class : foo_type) = object
      inherit some_parent_class
      ...
  end
  </code></pre>
  
  <p>Is this possible in OCaml?</p>
  
  <p>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.</p>
  
  <p>Edit: I think I have found a way to create the desired class using first-class modules:</p>
  
  <pre><code>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)
  </code></pre>
  
  <p>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.</p>