属性值变成"Method"里面Proxy.new(Raku)

Attribute Value Becomes "Method" Inside Proxy.new (Raku)

我试图理解为什么属性值在 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 确实指向一个匿名方法。只需在 FETCHSTORE 中添加 say $!string,您就会看到它包含一个匿名的 Block,正在打印 <anon>。 如果您想要处理属性,请检查 以了解如何完成。