Course of Raku / Advanced / Operators / Traits and pragmas 🆕 / Exercises

A custom trait

Problem

A custom trait can take an argument, not just be present or absent. Write a trait is role(...) that accepts a string and records, in a hash keyed by the subroutine’s name, the role assigned to that subroutine.

Apply is role('admin') to a subroutine login, then print the role stored for login.

Example

The program prints:

admin

Solution

See the solution

Course navigation

Solution: A writable parameter   |   Solution: A custom trait