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

In reply to Re: [raku] what accessors are actually autogenerated for @.array attributes? by b2gills
in thread [raku] what accessors are actually autogenerated for @.array attributes? by tomgracey

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.