微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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行的街区

事实上,文档说:

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方法.

原文地址:https://www.jb51.cc/Perl/171356.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐