develooper Front page | perl.perl6.users | Postings from July 2021

Re: How do a pe-salt an array inside an object?

Thread Previous | Thread Next
From:
Clifton Wood
Date:
July 13, 2021 04:06
Subject:
Re: How do a pe-salt an array inside an object?
Message ID:
CADaz7G=JS_fyH-Z5jH1zigtykkVrQqZr6gU6UGF+HAqphFXjiw@mail.gmail.com
@ToddandMargo wrote:

Now I understand.  You are using Str as a
>      skip, skip, drop 2, skip, drop 4
> This is a sequential workaround.
> Also I was confused as I though Str was only
> a type declaration and could be used this way.
> What I would like to see is direct access, without the
> skips.
> In other words, the pre-salt equivalent of
> class AA { has Str @.I is rw };
> my $CC = AA.new;
> $CC.I[400]  = "four hundred"
> $CC.I[2000] = "two thousand"
> writing out 2000 skips is not practical.


I definitely agree with you, so let's use what we know of Raku to our
advantage. A hash structure would work well, here with the
Array-Indexes-to-Use as keys:

class AA {
  has @.I;

  submethod BUILD (:$data) {
     @!I[.key] = .value for $data[]
  }

  method new (*@data) {
    self.bless( data => @data )
  }
}

You can now do the following:

AA.new(2 => 'b', 4 => 66, 9 => 'apples')

Which makes for a nice initializer!

- X

Thread Previous | Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About