jjm*_*elo 7 oop perl6 dispatch
我正在尝试为X::NYI
该类编写一个示例作为对此问题的响应.我想出了这样的事情:
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;
Run Code Online (Sandbox Code Playgroud)
这是尝试重新实现方法调用postfix运算符为所调用的任何东西抛出异常.这不起作用:
No such method 'newish' for invocant of type 'Nothing'
Run Code Online (Sandbox Code Playgroud)
在NYI.p6第13行的街区
事实上,文档说:
从技术上讲,不是真正的运营商; 它的语法特殊于编译器.
这很可能意味着它无法被覆盖.这也意味着做我想做的事情意味着与元模型交互以拦截类解析方法.但我真的不知道如何做到这一点.在Rakudo源大部分的例子,比如这一个,抛出该异常时的具体函数被调用,并且,事实上,我们看到的异常被抛出dispatch
在方法Mu
层面.
那么最重要的dispatch
是做出这种事情的正确方法吗?还是别的什么完全不同?
感觉对你想要的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,
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
Run Code Online (Sandbox Code Playgroud)
请注意,我还使用了is hidden-from-backtrace
特征来确保FALLBACK
在回溯中没有提到该方法.
归档时间: |
|
查看次数: |
82 次 |
最近记录: |