September 2017
Beginner
402 pages
9h 52m
English
The value of $.rooms attribute is set at the moment of creating the object $house. What if we need to change its value later? Of course, it is a rare case with real houses that the number of rooms changes but it still may happen, after a new owner takes the house and changes its floor plan, for example.
A naïve attempt to set a new value fails:
class House { has $.rooms;}my $house = House.new(rooms => 2);$house.rooms = 3; # Fails heresay $house.rooms;
At the time of assignment, a runtime error occurs and the program is terminated:
Cannot modify an immutable Int in block <unit> at house.pl line 7
The $.rooms attribute is immutable and cannot be changed. This is the default behavior of class attributes.
To make the ...