role Metamodel::MethodDelegation
role Metamodel::MethodDelegation { }
Warning: this role is part of the Rakudo implementation, and is not a part of the language specification.
Methods of Any and Mu can be invoked on roles, despite them not actually having these types as parents:
role Role { }
say Role.raku; # OUTPUT: «Role»
say Role.^pun.^parents(:all).map(*.^name); # OUTPUT: «()»
Metamodel::MethodDelegation is the metarole responsible for this
behavior. Using the metamethods this provides, metaobjects can delegate
method dispatch to another type object. This can be useful when
implementing types that shouldn't store methods of their own through
Metamodel::MethodContainer, but
should still support method dispatch somehow.
All this metarole does is provide an interface for storing a type object
to delegate methods to and provide a default find_method method for
delegating method lookups to that type object if no other method lookup
behavior for it exists; any other behavior related to methods is left up
to the metaclasses that do this metarole to implement themselves.
Because method delegation is a property of the metaclass for a HOW, not
HOWs themselves, the delegate_methods_to and
delegating_methods_to metamethods this metarole provides must be
invoked directly through a metaclass or HOW, not with .^ syntax:
say Role.HOW.delegating_methods_to.^name; # OUTPUT: «Any»
This metarole is commonly used in combination with Metamodel::TypePretense.
Methods
method delegate_methods_to
method delegate_methods_to($type)
Delegates methods to $type. This should be a type object, but may be
any object with a find_method metamethod technically.
method delegating_methods_to
method delegating_methods_to()
Returns the type object a metaobject is delegating methods to.
method find_method
method find_method($obj, $name)
Looks up a method on the type object this metaobject is delegating method dispatch to.