|
|
| Think about Loose Coupling | |
| PerlMonks |
Re: [Raku] Assigning defaults to attributes that are fixed length lists (and other confusions)by duelafn (Parson) |
| on Apr 28, 2021 at 13:01 UTC ( [id://11131790]=note: print w/replies, xml ) | Need Help?? |
|
If in your first attempt (with unless @!items) you instead use: say "Items: " ~ $hamper.items.perl; you will get
The code has @.items[3] is rw; creates a positional attribute which contains a fixed-length array with 3 elements. Thus, the array is truthy and the unless is not triggered. When you change to @!items := ('Mince Pie', 'White Wine', 'Stinky Cheese', 'Sardines', 'Dogfood'); you replace the fixed-kength array with a new List. Since @!items is rw that is allowed -- you aren't modifying the fixed length array, you are replacing it. Since the [3] describes the array, not the attribute, this is all fine -- to raku, though not perhaps to you :). There may be a way to do this using has, but at some point it is reasonable to write your own accessor:
Good Day,
In Section
Seekers of Perl Wisdom
|
|
||||||||||||||||||||||||||||||||||||||||