Metamodel::MethodContainer Reference
Metaobject that supports storing and introspecting methods
What it is #
Position in the hierarchy #
| Inherits from | — |
| Does | — |
| Consumed by | Metamodel::ClassHOW, Metamodel::ConcreteRoleHOW, Metamodel::EnumHOW, Metamodel::ParametricRoleHOW |
Routines #
Signatures are reproduced from the documentation, including their declared return types. A routine listed here is part of the type's published interface; whether Raku++ implements it is a separate question, answered by the examples below.
method add_method #
method add_method($obj, $name, $code)
Adds a method to the metaclass, to be called with name $name. This should only be done before a type is composed.
method methods #
method methods($obj, :$all, :$local)
Returns a list of public methods available on the class (which includes methods from superclasses and roles). By default this stops at the classes Cool, Any or Mu; to really get all methods, use the :all adverb. If :local is set, only methods declared directly in the class are returned. The returned list contains objects of type Method, which you can
method method_table #
method method_table($obj --> Hash:D)
Returns Hash:D.
Returns a hash where the keys are method names, and the values are Methods. Note that the keys are the names by which the methods can be called, not necessarily the names by which the methods know themselves.
method lookup #
method lookup($obj, $name --> Method)
Returns Method.
Returns the first matching Method object of the provided $name or (Mu) if no method object was found. The search for a matching method object is done by following the mro of $obj. Note that lookup is supposed to be used for introspection, if you're after something which can be invoked you probably want to use find_method
Examples, run three ways #
Every example below comes from the official documentation, together with the output that documentation asserts. Each was then executed by Rakudo and by Raku++ when this page was built. Where the three agree, one result is shown; where they do not, all three are — because which of them is wrong is exactly the information worth having.
1 all-differ · 4 no-output · 1 ok
role Metamodel::MethodContainer {}Not executed: the documentation states no expected output for this example.
say .name for Int.^methods(:all);
Not executed: the documentation states no expected output for this example.
# don't do that, because it changes type Int globally.
# just for demonstration purposes.
Int.^add_method('double', method ($x:) { 2 * $x });
say 21.double; # OUTPUT: «42»42
Documentation, Rakudo and Raku++ all agree.
class A {
method x() { };
}Not executed: the documentation states no expected output for this example.
say A.^methods(); # x say A.^methods(:all); # x infinite defined ...
Not executed: the documentation states no expected output for this example.
say 2.5.^lookup("sqrt").raku; # OUTPUT: «method sqrt (Rat $: *%_) ...»
say Str.^lookup("BUILD").raku; # OUTPUT: «submethod BUILD (Str $: :$value = "", *%_ --> Nil) ...»
say Int.^lookup("does-not-exist"); # OUTPUT: «(Mu)»method sqrt (Rat $: *%_) ...
submethod BUILD (Str $: :$value = "", *%_ --> Nil) ...
(Mu)
proto method sqrt (Cool $:: *%_) {*}
submethod BUILD (Str $:: Str(Any) :$value = "", *%_ --> Nil) { #`(Submethod|3339510199248) ... }
(Mu)
&sqrt
&BUILD
&does-not-exist
All three differ. Needs a human.
Not yet examined. Which of these is correct has not been established — do not treat either engine as settled here.