为混合角色的属性分配值 [英] Assigning a value to the attribute of a mixed-in role

查看:85
本文介绍了为混合角色的属性分配值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用的示例Perl 6中的Enumeration角色(作为解决doc问题的一部分枚举角色不是记录).我想出了一个简单的例子:

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;
}

从理论上讲,Enumeration has $!index,并尝试使用index => 33为其分配值;但是,它返回的只是类似

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

任何其他直接为$!index赋值的方法,我得到答案之一;在这种情况下,我想知道是否有解决方法.

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.

推荐答案

这是一个错误

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

我不知道解决方法.

这篇关于为混合角色的属性分配值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆