Course of Raku / Objects, I/O, and exceptions / Classes and objects / Introspecting objects / Exercises / Count the attributes

Solution: Count the attributes

Here is a possible solution to the task.

Code

class Point {
    has $.x;
    has $.y;
}

say Point.^attributes.elems;

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

Output

2

Comments

  1. .^attributes returns the list of the class’s attributes.

  2. .elems counts them. The Point class declares two attributes, so the answer is 2.

Course navigation

Count the attributes   |   The metaobject protocol 🆕