in reply to [raku] what accessors are actually autogenerated for @.array attributes?
When you declare a public attribute (with the . twigil) you are telling it (among other things) to create a method with the same name.
class Fish { has @.scales; }
class Fish { has @!scales; method scales () { return @!scales } # other stuff it does (not exhaustive) submethod BUILD ( :@!scales ){} method gist () { … } method Capture () { \( :@!scales ) } }
When you declare it as is rw, you are telling it that the generated method should also be marked as is rw.
class Fish { has @.scales is rw; }
class Fish { has @!scales; # v---v method scales () is rw { return-rw @!scales } # other stuff it does (not exhaustive) submethod BUILD ( :@!scales ){} method gist () { … } method Capture () { \( :@!scales ) } }
So then when you assign to the method, you replace the contents of the array.
my $fish = Fish.new; $fish.scales = ('green','blue','yellow')
If you instead wanted to handle it with the parameter list, you would replace the generated one with a method you created yourself.
(You still want to make it public with . so that the other stuff happens)
class Fish { has @.scales; multi method scales () { @!scales } multi method scales ( +@new ) { @!scales = @new } } my $fish = Fish.new; $fish.scales('green','blue','yellow'); say $fish.scales(); # does not clear it $fish.scales(()); # clears it
You can do it with an only method if you prefer.
class Fish { has @.scales; method scales ( |c (+@new) ) { @!scales = @new if c; return @!scales; } } my $fish = Fish.new; $fish.scales('green','blue','yellow'); say $fish.scales(); # does not clear it $fish.scales(()); # clears it
The |c is necessary so that an empty list given as the first and only argument will clear @!scales, but a truly empty argument list doesn't.
There is one thing that you should know, Array and Hash attributes currently don't need is rw to be mutable. They are mutable already.
class Fish { has @.scales; } my $fish = Fish.new; $fish.scales = ('green','blue','yellow'); say $fish.scales(); $fish.scales = (); # clears it
|
|---|