my House $a does the same as my Int $a . This limits the values ββyou can insert into it. If you look at the contents of a variable, you get an object like this constraint.
There is a trick you can use, so you donβt need to repeat the House bit: my House $a .= new , which is equivalent to my House $a = House.new .
To get back to your question: yes, you can do this with some problems:
class House { has $.area; multi method area(House:U \SELF:) is raw { (SELF = House.new).area } multi method area(House:D:) is raw { $!area } } my House @houses; @houses[2].area = 42; say @houses # [(House) (House) House.new(area => 42)]
We create two candidates for the access method: one accepts an object of type undefined, and the other an instance of the object. The first modifies its caller (provided that it is a container that can be installed), and then calls an instance of the method. I leave this as an exercise for the reader to turn this into an Attribute trait.
Elizabeth Mattijsen
source share