当原始变量具有默认强制时,有没有办法访问混合组件?
例如,在这种情况下:
my @list = (2,) but "bar";
put @list.Str «2?»
似乎没有办法访问“bar”组件。我错过了什么吗?同样会发生,例如,Set
my @list = (2,3) but Set(4,5);
put @list.Set; # OUTPUT: «3 2?»
回答
赋值是一个复制操作,所以:
my @a = something;
创建一个Arrayin @a,迭代something并将每个元素存储在 中@a。如果改为使用绑定:
my @list := (2,) but "bar";
put @list.Str;
然后将带有 mixin 的列表绑定到符号@list,输出为:
bar
- No, it goes on the `List` as a whole. In the case where we assign, the `List` on the right is only used for its contents, then becomes unreachable.
-
`$something but "a string"` is short for `$something but role { method Str() { "a string" } }` - that is, it changes how the object will coerce to a `Str`. The `print` function coerces its argument to `Str`. The `put` function - short for "print using terminator" - also does, since it's a form of `print`. By contrast, `say` calls the `gist` method, as in "give me [the gist of it](https://dictionary.cambridge.org/dictionary/english/gist)". The role we
mixed in changes nothing about that. - But that means that the mix-in goes to the first element?