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

Read-write attributes

By default, the accessor created by has $.name is read-only. The value is initialised in the constructor, after which you can read the value but not change it from outside the object. Assigning to it is an error:

class Dog {
    has $.name;
}

my $rex = Dog.new(name => 'Rex');
$rex.name = 'Max';

This stops with:

Cannot modify an immutable Str (Rex)

To allow the value to be changed through the accessor, mark the attribute with the is rw trait:

class Dog {
    has $.name is rw;
}

my $rex = Dog.new(name => 'Rex');
$rex.name = 'Max';
say $rex.name; # Max

Now the accessor returns a writable container, so the assignment works and the object’s name becomes Max.

Course navigation

Attributes   |   Default values


💪 Or jump directly to the exercises in this section.