我正在尝试为
@H_404_21@解决方法
X::NYI
课程编写一个示例作为对
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交互以拦截类解析方法.但我真的不知道如何做到这一点. Rakudo源代码中的大多数示例(如this one)在调用具体函数时抛出异常,实际上是the exception we see is thrown by the dispatch
method at the Mu
level.
那么压倒派遣是否是做这种事情的正确方法?还是别的什么完全不同?
感觉对你我想要FALLBACK:
https://docs.perl6.org/language/typesystem#index-entry-FALLBACK_%28method%29
这将转化为:
class Nothing { method FALLBACK($name,|c) is hidden-from-backtrace { die X::NYI.new( feature => $name,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
请注意,我还使用了隐藏的回溯特性来确保在回溯中没有提到FALLBACK方法.