Course of Raku / Objects, I/O, and exceptions / Classes and objects / Classes / Exercises / Defined or not

Solution: Defined or not

Here is a possible solution to the task.

Code

class Cat {
}

class Dog {
}

for Cat, Dog, Cat.new, Dog.new -> $thing {
    say $thing.defined;
}

🦋 You can find the source code in the file defined-or-not.raku.

Output

False
False
True
True

Comments

  1. The loop walks over a mix of type objects (Cat, Dog) and instances (Cat.new, Dog.new).

  2. Both type objects are undefined, so defined returns False for them; both instances are defined, so it returns True. Definedness depends on whether a value is a type object or a real instance — not on which class it belongs to.

Course navigation

Defined or not   |   Name the type