methods - Proxy.new (Raku) 中的属性值变为 "Method"

标签 methods proxy attributes 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真的是指向一个匿名方法。只需添加 say $!string在您的 FETCHSTORE你会看到它包含一个匿名 Block , 打印<anon> .
如果您想要处理属性,请检查 this other SO answer看看怎么做。

关于methods - Proxy.new (Raku) 中的属性值变为 "Method",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65360737/

相关文章:

java - 在 Java 中转发 HTTP 请求

proxy - nginx 1.2.0 - socket.io - HTTP/1.1 - 代理 websocket 连接

c# - 使用属性生成自定义 setter

attributes - MSTest:当网络不可用时忽略依赖网络的集成测试

C++类方法线程

Java随机数问题。

java - 使变量可以被所有方法访问

swift - Swift 中的属性闭包和方法有什么区别?

java - 创建一个无操作 IncallingHandler

php - DomDocument 删除属性