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
TrueComments
The loop walks over a mix of type objects (
Cat,Dog) and instances (Cat.new,Dog.new).Both type objects are undefined, so
definedreturnsFalsefor them; both instances are defined, so it returnsTrue. 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 →