我试图理解为什么属性值是 Proxy.new 之外的 Str(或其他),但成为 Proxy.new 内部的 Method。我把我的代码归结为:
#!/usr/bin/env raku
class Foo does Associative {
has Str $.string;
multi method AT-KEY (::?CLASS:D: Str $key) is rw {
say "\nOutside of Proxy: { $!string.raku }";
say "Outside of Proxy: { $!string.^name }";
Proxy.new(
FETCH => method () {
say "FETCH: { $!string.raku }";
say "FETCH: { $!string.^name }";
},
STORE => method ($value) {
say "STORE: { $!string.raku }";
say "STORE: { $!string.^name }";
}
);
}
}
my $string = 'foobar';
my %foo := Foo.new: :$string;
%foo<bar> = 'baz';
say %foo<bar>;
这是输出:$ ./proxy.raku
Outside of Proxy: "foobar"
Outside of Proxy: Str
STORE: method <anon> (Mu: *%_) { #`(Method|94229743999472) ... }
STORE: Method
Outside of Proxy: "foobar"
Outside of Proxy: Str
FETCH: method <anon> (Mu: *%_) { #`(Method|94229743999616) ... }
FETCH: Method
FETCH: method <anon> (Mu: *%_) { #`(Method|94229743999616) ... }
FETCH: Method
FETCH: method <anon> (Mu: *%_) { #`(Method|94229743999616) ... }
FETCH: Method
FETCH: method <anon> (Mu: *%_) { #`(Method|94229743999616) ... }
FETCH: Method
FETCH: method <anon> (Mu: *%_) { #`(Method|94229743999616) ... }
FETCH: Method
FETCH: method <anon> (Mu: *%_) { #`(Method|94229743999616) ... }
FETCH: Method
FETCH: method <anon> (Mu: *%_) { #`(Method|94229743999616) ... }
FETCH: Method
FETCH: method <anon> (Mu: *%_) { #`(Method|94229743999616) ... }
FETCH: Method
FETCH: method <anon> (Mu: *%_) { #`(Method|94229743999616) ... }
FETCH: Method
True
谁能解释一下这里发生了什么?谢谢!
最佳答案
问题是$!string
在块被实际调用时是一个空容器。即使它在作用域中声明,到实际使用时 $!str
真的是指向一个匿名方法。只需添加 say $!string
在您的 FETCH
或 STORE
你会看到它包含一个匿名 Block
, 打印<anon>
.
如果您想要处理属性,请检查 this other SO answer看看怎么做。
关于methods - Proxy.new (Raku) 中的属性值变为 "Method",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65360737/