Course of Raku / Objects, I/O, and exceptions / Classes and objects / The metaobject protocol 🆕 / Meta-methods

Quiz — The MOP

What does the following program print?

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

say Point.^attributes.elems;
01
12
0(Point)
0an error

.^attributes is a meta-method — called with .^ on the type’s metaobject — that lists the attributes a type declares, and .elems counts them. Point declares two attributes, $.x and $.y, so it prints 2.

Course navigation

Meta-methods   |   Adding methods at runtime