Course of Raku / Objects, I/O, and exceptions / Classes and objects / The metaobject protocol 🆕 / Exercises / A diamond of classes
Solution: A diamond of classes
Here is a possible solution to the task.
Code
class A {
}
class B is A {
}
class C is A {
}
class D is B is C {
}
say D.^mro.map(*.^name);🦋 You can find the source code in the file diamond-mro.raku.
Output
(D B C A Any Mu)Comments
Dinherits from two parents at once —class D is B is Clists each of them with its ownis. This is multiple inheritance, andA,B,C,Dform a diamond: two paths fromDup to the shared ancestorA..^mroflattens that diamond into a single, linear search order.Dcomes first, then its parentsBandCin the order they were written, then the sharedA, and finallyAnyandMu.Even though both
BandClead toA, the typeAappears once, and only after both of them. That is the point of a method resolution order: every type is visited exactly once, and a parent never comes before a child that inherits from it — so a method defined inBis always found before the one it would override inA.