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

p6s*_*eve 8 oop raku

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

现在我正在明确地这样做......

# 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
    }
}
Run Code Online (Sandbox Code Playgroud)

但是,我想通过“俚语方法调用”来做到这一点,就像这样编写代码......

# 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")'
    ...
}

Run Code Online (Sandbox Code Playgroud)

(如何)这可以在 raku 中完成吗?

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

p6s*_*eve 6

根据 @jonathans 评论,raku文档指出:

\n
\n

当其他解析名称的方法没有产生结果时,将调用具有特殊名称 FALLBACK 的方法。第一个参数保存名称,所有后续参数均从原始调用转发。支持多种方法和子签名。

\n
\n
class Magic {\n    method FALLBACK ($name, |c(Int, Str)) {\n    put "$name called with parameters {c.raku}"  }\n};\nMagic.new.simsalabim(42, "answer");\n \n# OUTPUT: \xc2\xabsimsalabim called with parameters \xe2\x8c\x88\\(42, "answer")\xe2\x8c\x8b\xe2\x90\xa4\xc2\xbb\n
Run Code Online (Sandbox Code Playgroud)\n

所以我的代码示例将如下所示:

\n
# callee side\nclass Pd-Stub {\n    method FALLBACK ($name, Str $arg-str ) {\n        say "$name called with args: <<$arg-str>>"\n    }\n}\n\nclass Series { \n    has Pd-Stub $.pd \n}\n\nmy \\s = Series.new;\n\n# caller side\ns.pd.shape;                 #shape called with args: <<>>        \ns.pd.to_json("test.json");  #to_json called with args: <<test.json>>\ns.pd.iloc[2] = 23;          #iloc called with args: <<>>\n\n#iloc needs to use AT-POS and Proxy to handle subscript and assignment\n
Run Code Online (Sandbox Code Playgroud)\n