If you create a class:
class Foo { }
the class will inherit all of its methods from Any
, and then Mu
.
I want to create a class that doesn't inherit from any other class: it should contain a single FALLBACK
method that should catch all method calls to instances of that object.
I've looked through the MetaModel
code, but there does not seem to be a simple way to achieve this goal. All suggestions are welcome!
UPDATE: I decided to go the intercept any method call way as described by Jonathan Worthington. This resulted in two new Perl 6 modules on CPAN: InterceptAllMethods and Object::Trampoline.
This is possible, although you're likely to run into practical problems that will require further effort. Calling construction logic is a good example already pointed out in a comment. Further to that, everything is expected to successfully type check against
Mu
; such checks are elided in most places as an optimization, but not others, and so you can expect to run into assorted type check failures.With that aside, here's how to do it. First, create a module which exports a new meta-type for
class
.The metamodel has to somehow be used to set up the
Mu
type in the first place, and so here we (ab)use a mechanism that ordinarily means "no, there's no default parent type yet because we didn't bootstrap our object model that far". Stick this into a module, say calledParentless
, and then it's possible to do this:Which outputs:
If your goal is simply to intercept every method dispatch, there's a far less disruptive way that doesn't mess with the type system. For the moment it needs a custom metaclass that disables method cache publication:
You can then write:
Note that unlike
FALLBACK
, here you return a code object that will then be invoked. You can write thisfind_method
implementation in the metaclass too, which may be a better factoring; it's hard to say without knowing the problem at hand.This approach will cause no type-check related issues, let you intercept every method dispatch, and it's easy enough to look up things like
bless
and just delegate those to theMu
implementation.