Dear OCaml community,
Let say I have two classes
b that define methods
b#m (we can suppose that these methods overload the method
O#m from a common ancestor
o of the classes
b if it helps).
I can define a class
a_enriched, where I overload again the method m for composing its result with a given function f.
class a_enriched f = object inherit a as super method! m = f super#m end
I can do the same with
class b_enriched f = object inherit b as super method! m = f super#m end
The code that defines
b_enriched is the same apart from the name of the classes.
Can I define the classes
b_enriched without repeating most of their definitions? In my actual settings, I don’t have only one single method
m but several methods to overload, and I would like be able to write things more concisely.
To give more context if it can help to find the correct solution, the two classes
B are called in my particular use case
lift_pattern, and derive from “lifters” obtained with the
traverse_lift deriver from ppxlib.
class lift_expr loc = object(self) inherit [Parsetree.expression] Clang__bindings.lift ... end class lift_pattern loc = object(self) inherit [Parsetree.pattern] Clang__bindings.lift ... end
and the code in
... is substantially different between the two classes.
My question comes from the need to enrich the behavior of both lifters by open recursion. Something like:
class lift_expr_enriched f loc = object inherit lift_expr loc as super method! qual_type e = match e with | <thing of interest>(x) -> f x | _ -> super#qual_type e end class lift_pattern_enriched f loc = object inherit lift_pattern loc as super method! qual_type e = match e with | <thing of interest>(x) -> f x | _ -> super#qual_type e end
and I would like to write most of the duplicated code once.