Don't ask to ask, just ask | |
PerlMonks |
comment on |
( [id://3333]=superdoc: print w/replies, xml ) | Need Help?? |
Posting a quick update to this as my new and improved understanding of attributes led me to a simple alternative which gave me what I wanted. I realised my main problem was the unless having the wrong effect. The phenomenon of overwriting the fixed length array with a longer one is really just a curiosity, since I don't think there's any practical situation when you'd define an N sized array, and then specify an N + 1 element array as the default. I think the unless issue can be resolved easily in either of 2 ways, depending on what effect is desired:
This simply has code in TWEAK assign defaults on a per element basis. This way you get defaults at the element level, so e.g. if you do this:
$hamper.items will end up with the user specified values Fish and Canoe as the first 2 elements, and Stinky Cheese as the 3rd. This might be good for some purposes, but in my case I wanted the defaults to be completely overwritten if the user specified any values for @!items at all. ie in the above example, asking for $hamper.items should return "uninitialized value" for the third item. Only if I don't try to set any items at all should I get the defaults. This seems quite straightforward to implement with grep:
This now achieves everything I hoped:
I think if you want exactly 3 (or however many) elements, then writing your own accessor is very likely your best option. (Or perhaps subclassing Array? Not really sure of the details there) Anyway, I just thought I'd share in case it helps anyone else out... In reply to Re^3: [Raku] Assigning defaults to attributes that are fixed length lists (and other confusions)
by tomgracey
|
|