txt: ## methodcontainer.md
  role Metamodel::MethodContainer
  
  Metaobject that supports storing and introspecting methods
  
  ```raku
  role Metamodel::MethodContainer {}
  ```
  
  *Warning*: this role is part of the Rakudo implementation, and is not a part of the language specification.
  
  Roles, classes, grammars and enums can contain methods. This role implements the API around storing and introspecting them.
  
  ```raku
  say .name for Int.^methods(:all);
  
  # 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␤»
  ```
  
  # [Methods](#role_Metamodel::MethodContainer "go to top of document")[§](#Methods "direct link")
  
  ## [method add\_method](#role_Metamodel::MethodContainer "go to top of document")[§](#method_add_method "direct link")
  
  ```raku
  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](#role_Metamodel::MethodContainer "go to top of document")[§](#method_methods "direct link")
  
  ```raku
  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`](/type/Cool), [`Any`](/type/Any) or [`Mu`](/type/Mu); to really get all methods, use the `:all` adverb. If `:local` is set, only methods declared directly in the class are returned.
  
  ```raku
  class A {
      method x() { };
  }
  
  say A.^methods();                   # x
  say A.^methods(:all);               # x infinite defined ...
  ```
  
  The returned list contains objects of type [`Method`](/type/Method), which you can use to introspect their signatures and call them.
  
  Some introspection method-look-alikes like [`WHAT`](/language/mop#WHAT) will not show up, although they are present in any Raku object. They are handled at the grammar level and will likely remain so for bootstrap reasons.
  
  ## [method method\_table](#role_Metamodel::MethodContainer "go to top of document")[§](#method_method_table "direct link")
  
  ```raku
  method method_table($obj --> Hash:D)
  ```
  
  Returns a hash where the keys are method names, and the values are [`Method`](/type/Method)s. 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](#role_Metamodel::MethodContainer "go to top of document")[§](#method_lookup "direct link")
  
  ```raku
  method lookup($obj, $name --> Method)
  ```
  
  Returns the first matching [`Method`](/type/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](/type/Metamodel/C3MRO) 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](/routine/find_method) instead.
  
  ```raku
  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)␤»
  ```
  
  The difference between `find_method` and `lookup` are that `find_method` will use a default candidate for parametric roles, whereas `lookup` throws an exception in this case, and that `find_method` honors `FALLBACK` methods, which `lookup` does not.
  