我正在尝试使用EnumerationPerl 6中的角色的一个示例(作为修复文档问题的一部分,枚举角色未记录).我想出了这个简单的例子:
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;
}
Run Code Online (Sandbox Code Playgroud)
从理论上讲,Enumeration has $!index和与index => 33我尝试值分配给它; 然而,它返回的就像是
Pairs T => A with T -> A with 0
Run Code Online (Sandbox Code Playgroud)
任何其他方式直接为$!index赋值,并且我得到"无法分配给我在另一个问题中得到的不可变值.根据其中一个答案,这可能是一个错误;在这种情况下,我会想知道是否有一些解决方法.