Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

[Raku] Assigning defaults to attributes that are fixed length lists (and other confusions)

by tomgracey (Scribe)
on Apr 27, 2021 at 20:34 UTC ( [id://11131766]=perlquestion: print w/replies, xml ) Need Help??

Help for this page

Select Code to Download


  1. or download this
        class Hamper {
            has $.name = 'Christmas Basket';
    ...
    
        say "Name: " ~ $hamper.name;
        say "Items: " ~ $hamper.items;
    
  2. or download this
        Name: Christmas Basket
        Items: Mince Pie White Wine Stinky Cheese
    
  3. or download this
        has @.items[3] is rw = 'Mince Pie', 'White Wine', 'Stinky Cheese';
    
  4. or download this
    ===SORRY!=== Error while compiling /path/script.raku
    Defaults on compound attribute types not yet implemented. Sorry.
    ...
            @!items := (initial values) unless @!items;
        }
    at /path/script.raku:3
    
  5. or download this
    class Hamper {
        has $.name = 'Christmas Basket';
    ...
    
    say "Name: " ~ $hamper.name;
    say "Items: " ~ $hamper.items;
    
  6. or download this
    Name: Christmas Basket
    Use of uninitialized value element[0] 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 script.raku line 16
    Items:
    
  7. or download this
    Name: Christmas Basket
    Items: Mince Pie White Wine Stinky Cheese
    
  8. or download this
    my $hamper = Hamper.new( items => ('Dog', 'Cat', 'Sausage') );
    
  9. or download this
    Name: Christmas Basket
    Items: Mince Pie White Wine Stinky Cheese
    
  10. or download this
    method TWEAK(){
        @!items := ('Mince Pie', 'White Wine', 'Stinky Cheese', 'Sardines'
    +, 'Dogfood');
    }
    
  11. or download this
    Name: Christmas Basket
    Items: Mince Pie White Wine Stinky Cheese Sardines Dogfood
    
  12. or download this
        has Str @.items[3];
    
  13. or download this
    class Hamper {
        has $.name = 'Christmas Basket';
    ...
    
    say "Name: " ~ $hamper.name;
    say "Items: " ~ $hamper.items;
    
  14. or download this
    Type check failed in binding; expected Positional[Str] but got List ((
    +"Mince Pie", "White...)
      in method TWEAK at script.raku line 9
      in block <unit> at script.raku line 13
    

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://11131766]
Approved by erzuuli
Front-paged by erzuuli
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others goofing around in the Monastery: (5)
As of 2024-04-19 16:19 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found