覆盖方法调用运算符或其他一些方法来捕获方法名称解析错误

Overriding method call operator or some other way to catch method name resolution errors

我正在尝试为 X::NYI class as a response to this issue 编写示例。我想到了这样的事情:

class Nothing {
    sub postfix:<.&>( $sub, **@args) {
        die X::NYI.new( feature => $sub,
                        did-you-mean => "nothing",
                        workaround => "Implement it yourself" );
    }
}

my $let's-see = Nothing.newish;

它正在尝试重新实现 the method call postfix operator 以对调用的任何内容抛出异常。这不起作用:

No such method 'newish' for invocant of type 'Nothing'

在 NYI.p6 第 13 行的区块中

事实上,文档说:

Technically, not a real operator; it's syntax special-cased in the compiler.

这很可能意味着它不能被覆盖。这也意味着做我想做的事意味着与 metamodel to intercept the class resolution method. But I don't really see how this could be done. Most examples in Rakudo source, such as this one, throw the exception when a concrete function is called, and, in fact, the exception we see is thrown by the dispatch method at the Mu level 互动。

那么重写 dispatch 是做这种事情的正确方法吗?或者其他完全不同的东西?

我觉得你想要FALLBACK:

https://docs.raku.org/language/typesystem#index-entry-FALLBACK_%28method%29

这将转化为:

class Nothing {
    method FALLBACK($name, |c) is hidden-from-backtrace {
        die X::NYI.new( feature => $name,
                        did-you-mean => "nothing",
                        workaround => "Implement it yourself" );
    }
}

my $a = Nothing.newish;
============================
newish not yet implemented. Sorry.
Did you mean: nothing?
Workaround: Implement it yourself
  in block <unit> at file line 10

请注意,我还使用了 is hidden-from-backtrace 特性来确保 FALLBACK 方法未在回溯中提及。