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

不打扰是莪最后的温柔 提交于 2019-12-10 13:07:01

问题


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.



来源:https://stackoverflow.com/questions/51632394/assigning-a-value-to-the-attribute-of-a-mixed-in-role

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!