Course of Raku / Objects, I/O, and exceptions / Classes and objects / The metaobject protocol 🆕 / Exercises / Find a method

Solution: Find a method

Here is a possible solution to the task.

Code

class Animal {
    method speak { 'generic' }
}

class Dog is Animal {
}

say so Dog.^find_method('speak');

🦋 You can find the source code in the file find-method.raku.

Output

True

Comments

  1. .^find_method returns the method if it exists, or an undefined value if not. The so turns that into a plain Boolean.

  2. Dog defines no methods itself, yet the result is True: find_method searches the whole inheritance chain and finds speak up in Animal. This mirrors how an actual method call on a Dog would locate the inherited method.

Course navigation

Find a method   |   The method resolution order