Assigning a value to the attribute of a mixed-in r

2019-06-17 06:19发布

问题:

I'm trying to work an example that uses the Enumeration role in Perl 6 (as part of fixing doc issue Enumeration role is not documented). I came up with this simple example:

class DNA does Enumeration {
    my $DNAindex = 0;
    my %pairings = %( A => "T",
                      T => "A",
                      C => "G",
                      G => "T" );

    method new( $base-pair where "A" | "C" | "G" | "T" )  {
        self.bless( key => $base-pair,
                    value => %pairings{$base-pair},
                    index => 33);
    }

    multi method gist(::?CLASS:D:) {
        return "$!key -> $!value with $!index";
    }

}

for <A C G T>.roll( 16 ) -> $letter {
    my DNA $base = DNA.new( $letter );
    say "Pairs ", $base.pair,  " with ", $base.gist;
}

Theoretically, Enumeration has $!index, and with index => 33 I try to assign a value to it; however, all it returns is something like

 Pairs T => A with T -> A with 0

Any other way to assign a value to $!index directly, and I get the "cannot assign to an immutable value I got in another question. That might be a bug, according to one of the answers; in that case, I'd like to know if there's some workaround to it.

回答1:

It's a bug Cannot change native role attribute from consuming class (nothing to do with the one mentioned in the answer you linked).

I don't know of a workaround.



标签: mixins perl6