Hi Tom... with you on the SO tone ... here's what I think is going on:

  1. An empty Array is falsey, yet an empty shaped Array is truthy (I would be interested to know why!) - thus, as you have already noted, the original error message is wrong in the suggestion for TWEAK. (You may wish to raise a bug issue at rakudostar on git.) More at https://stackoverflow.com/questions/67373726
  2. You are getting a compiler warning from trying to say ~$hamper.items where there is an empty value (Any) in a shaped Array... this does not happen with a non-shaped Array as the length auto-adjusts to the contents.
    Use of uninitialized value element[2] of type Any in string context. Methods .^name, .raku, .gist, or .say can be used to stringify it to s +omething meaningful. in block <unit> at test.raku line 14
    So, I propose you implement a Str method to bypass (Any) elements and format the object output for .put (or say ~$hamper).
  3. Array elements are each scalar containers and are each rw in any case - has @.items is rw just allows the top level array to be assigned to via a generated setter method. (NB. TWEAK can use the private @!items variable directly to assign the defaults).
  4. You can see this now gives low boilerplate accessors that do what you want (?) and protect from writing beyond 3 elements.
class Hamper { has $.name = 'Christmas Basket'; has @.items[3]; method TWEAK { @!items = ['Mince Pie', 'White Wine', 'Stinky Cheese'] unless +@!items.any.so; } method Str { "Name: {$!name}\nItems: [{@!items.grep(*.so).join(', ')}]\n"; } } given Hamper.new { .items[2] = 'Hard Cheese'; .put; } #Name: Christmas Basket #Items: [Mince Pie, White Wine, Hard Cheese] given Hamper.new( items => ['Fish', 'Canoe'] ) { .put; .items[2] = 'Bicycle'; .put; #.items[3] = 'Horse'; #fails.. Index 3 for dimension 1 out of +range (must be 0..2) #.items.push: 'Blue Nun'; #fails.. Cannot push a fixed-dimension a +rray } #Name: Christmas Basket #Items: [Fish, Canoe] #Name: Christmas Basket #Items: [Fish, Canoe, Bicycle]

In reply to Re^4: [Raku] Assigning defaults to attributes that are fixed length lists (and other confusions) by p6steve
in thread [Raku] Assigning defaults to attributes that are fixed length lists (and other confusions) 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.