Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Overriding method call operator or some other way to catch method name resolution errors

Tags:

oop

dispatch

raku

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?

like image 493
jjmerelo Avatar asked May 24 '18 07:05

jjmerelo


1 Answers

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.

like image 157
Elizabeth Mattijsen Avatar answered Nov 21 '22 03:11

Elizabeth Mattijsen