Course of Raku / Objects, I/O, and exceptions / Classes and objects / Introspecting objects / Exercises / Describe a class
Solution: Describe a class
Here is a possible solution to the task.
Code
class Animal {
}
class Dog is Animal {
}
say Dog.^name;
say Dog.^mro.elems;
say 'Cat' ∈ Dog.^mro.map(*.^name);🦋 You can find the source code in the file describe-a-class.raku.
Output
Dog
4
FalseComments
.^namereturns the class’s own name,Dog..^mroreturns the inheritance chain, and.elemscounts it. There are four types in the chain —Dog, its parentAnimal, and the universalAnyandMu— so the count is4..^mro.map(*.^name)turns that chain into the list of type names,(Dog Animal Any Mu). The∈set-membership operator then checks whetherCatis one of them.Dogdoes not descend from anyCat, so the answer isFalse.