oop – 重写方法调用操作符或其他一些方法来捕获方法名称解析错
我正在尝试为
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行的街区 事实上,文档说:
这很可能意味着它无法被覆盖.这也意味着做我想做的事情意味着与metamodel交互以拦截类解析方法.但我真的不知道如何做到这一点. Rakudo源代码中的大多数示例(如this one)在调用具体函数时抛出异常,实际上是the exception we see is thrown by the 那么压倒派遣是否是做这种事情的正确方法?还是别的什么完全不同? 解决方法
感觉对你我想要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方法. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |