Course of Raku / Objects, I/O, and exceptions / Classes and objects / Classes / Exercises / Name the type

Solution: Name the type

Here is a possible solution to the task.

Code

class Cat {
}

class Dog {
}

my $felix = Cat.new;

say $felix.WHAT;
say $felix.WHAT === Cat.WHAT;
say $felix.WHAT === Dog.WHAT;

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

Output

(Cat)
True
False

Comments

  1. WHAT returns the type object, shown in parentheses as (Cat)$felix is a Cat.

  2. === compares two type objects for identity. $felix.WHAT === Cat.WHAT is True because Felix really is a Cat, while $felix.WHAT === Dog.WHAT is False — a Cat and a Dog are different types, so their type objects are not the same.

Course navigation

Name the type   |   Attributes