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

相关文章

1. 如何去重 #!/usr/bin/perl use strict; my %hash; while(...
最近写了一个perl脚本,实现的功能是将表格中其中两列的数据...
表的数据字典格式如下:如果手动写MySQL建表语句,确认麻烦,...
巡检类工作经常会出具日报,最近在原有日报的基础上又新增了...
在实际生产环境中,常常需要从后台日志中截取报文,报文的形...
最近写的一个perl程序,通过关键词匹配统计其出现的频率,让...