I am trying to write an example for the X::NYI
class as a response to this issue. I have come up with something like this:
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;
Which is trying to reimplement the method call postfix operator to throw an exception for anything that is called. This does not work:
No such method 'newish' for invocant of type 'Nothing'
in block at NYI.p6 line 13
And, in fact, the documentation says:
Technically, not a real operator; it's syntax special-cased in the compiler.
Which most probably means it cannot be overridden. That also means that doing what I want implies interacting with the metamodel to intercept the class resolution method. But I don't really see how this could be done. Most examples in Rakudo source, such as this one, throw the exception when a concrete function is called, and, in fact, the exception we see is thrown by the dispatch
method at the Mu
level.
So would overriding dispatch
be the right way to do this kind of thing? Or something else completely different?
Feels to me you want FALLBACK
:
https://docs.raku.org/language/typesystem#index-entry-FALLBACK_%28method%29
which would translate to:
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
Note that I also used the is hidden-from-backtrace
trait to make sure the FALLBACK
method is not mentioned in the backtrace.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With