Course of Raku / Objects, I/O, and exceptions / Classes and objects / Classes

Type objects and instances

When you write a class name such as Dog, you are referring to the type object of the class. The type object represents the type itself, not any particular object of that type. An instance, created with new, is an actual object.

Raku can tell the two apart. The defined method returns False for a type object and True for an instance:

class Dog {
}

say Dog.defined;     # False
say Dog.new.defined; # True

A type object is considered undefined because it holds no concrete data — it is only the description of a type. An instance is defined: it is a real object.

Both report the same type through WHAT, which you met in the section about containers:

class Dog {
}

say Dog.WHAT;     # (Dog)
say Dog.new.WHAT; # (Dog)

The parentheses around (Dog) are Raku’s way of showing a type. So Dog and Dog.new share the type Dog; the difference is that one is the type itself, and the other is an object of that type.

Practice

Complete the quiz that covers the contents of this topic.

Course navigation

Classes   |   Quiz — Classes


💪 Or jump directly to the exercises in this section.