获取被调用者中名为 str 的方法

发布于 2025-01-17 14:18:10 字数 828 浏览 2 评论 0原文

我想从被调用方反思方法调用的尾部。

现在我正在明确地执行此操作...

# caller side
s.pd: '.shape';
s.pd: '.to_json("test.json")';
s.pd: '.iloc[2] = 23';

# callee side
method pd( Str $call ) {
    given $call {
        when /shape/   { ... }
        when /to_json/ { ... }
        #etc
    }
}

但是,我喜欢通过“俚语方法调用”的方式来执行此操作,就像这样编写的代码...

# caller side
s.pd.shape;
s.pd.to_json("test.json");
s.pd.iloc[2] = 23;
^ ^^ ^^^^^^^^^^^^^^^^^^^$
|  |       |
|  |       L a q// str I can put into eg. a custom Grammar
|  |
|  L general method name
|
L invocant

#callee side
method pd( ?? ) {
    my $called-as-str = self.^was-called-as;
    say $called-as-str;   #'pd.to_json("test.json")'
    ...
}

(如何)这可以吗用乐完成的?

由于在许多数量模式中需要处理 422 个调用,因此需要在被调用类中声明 422 个方法和签名的答案将不太有吸引力。

I would like to introspect the tail end of a method call from the callee side.

Right now I am doing this explicitly...

# caller side
s.pd: '.shape';
s.pd: '.to_json("test.json")';
s.pd: '.iloc[2] = 23';

# callee side
method pd( Str $call ) {
    given $call {
        when /shape/   { ... }
        when /to_json/ { ... }
        #etc
    }
}

BUT, I would like to do this by way of a 'slang method call', something like this made up code...

# caller side
s.pd.shape;
s.pd.to_json("test.json");
s.pd.iloc[2] = 23;
^ ^^ ^^^^^^^^^^^^^^^^^^^$
|  |       |
|  |       L a q// str I can put into eg. a custom Grammar
|  |
|  L general method name
|
L invocant

#callee side
method pd( ?? ) {
    my $called-as-str = self.^was-called-as;
    say $called-as-str;   #'pd.to_json("test.json")'
    ...
}

(HOW) can this be done in raku?

Since there are 422 calls to handle, of many arity patterns, answers that require the declaration of 422 methods and signatures in the called Class will be less attractive.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

放飞的风筝 2025-01-24 14:18:10

per @jonathans评论,raku docs state 状态:

使用特殊名称后返回的方法将在其他
意味着解决名称没有产生的结果。第一个论点成立
名称和所有以下论点都是从原始的
称呼。支持多种方法和子签名。

class Magic {
    method FALLBACK ($name, |c(Int, Str)) {
    put "$name called with parameters {c.raku}"  }
};
Magic.new.simsalabim(42, "answer");
 
# OUTPUT: «simsalabim called with parameters ⌈\(42, "answer")⌋␤»

因此,我的代码示例会读取:

# callee side
class Pd-Stub {
    method FALLBACK ($name, Str $arg-str ) {
        say "$name called with args: <<$arg-str>>"
    }
}

class Series { 
    has Pd-Stub $.pd 
}

my \s = Series.new;

# caller side
s.pd.shape;                 #shape called with args: <<>>        
s.pd.to_json("test.json");  #to_json called with args: <<test.json>>
s.pd.iloc[2] = 23;          #iloc called with args: <<>>

#iloc needs to use AT-POS and Proxy to handle subscript and assignment

Per @jonathans comment, the raku docs state:

A method with the special name FALLBACK will be called when other
means to resolve the name produce no result. The first argument holds
the name and all following arguments are forwarded from the original
call. Multi methods and sub-signatures are supported.

class Magic {
    method FALLBACK ($name, |c(Int, Str)) {
    put "$name called with parameters {c.raku}"  }
};
Magic.new.simsalabim(42, "answer");
 
# OUTPUT: «simsalabim called with parameters ⌈\(42, "answer")⌋␤»

So my code example would read:

# callee side
class Pd-Stub {
    method FALLBACK ($name, Str $arg-str ) {
        say "$name called with args: <<$arg-str>>"
    }
}

class Series { 
    has Pd-Stub $.pd 
}

my \s = Series.new;

# caller side
s.pd.shape;                 #shape called with args: <<>>        
s.pd.to_json("test.json");  #to_json called with args: <<test.json>>
s.pd.iloc[2] = 23;          #iloc called with args: <<>>

#iloc needs to use AT-POS and Proxy to handle subscript and assignment
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文