txt: ## callable.md
  role Callable
  
  Invocable code object
  
  ```raku
  role Callable { ... }
  ```
  
  Role for objects which support calling them. It's used in [`Block`](/type/Block), [`Routine`](/type/Routine), [`Sub`](/type/Sub), [`Method`](/type/Method), [`Submethod`](/type/Submethod) and [`Macro`](/type/Macro) types.
  
  Callables can be stored in `&`-sigiled containers, the default type constraint of such a container is `Callable`.
  
  ```raku
  my &a = {;}; # Empty block needs a semicolon
  my &b = -> {};
  my &c = sub () {};
  sub foo() {};
  my &d = &foo;
  ```
  
  # [Methods](#role_Callable "go to top of document")[§](#Methods "direct link")
  
  ## [method CALL-ME](#role_Callable "go to top of document")[§](#method_CALL-ME "direct link")
  
  ```raku
  method CALL-ME(Callable:D $self: |arguments)
  ```
  
  This method is required for the [`( )` postcircumfix operator](/language/operators#postcircumfix_(_)) and the [`.( )` postcircumfix operator](/language/operators#index-entry-.(_)). It's what makes an object actually call-able and needs to be overloaded to let a given object act like a routine. If the object needs to be stored in an `&`-sigiled container, it has to implement Callable.
  
  ```raku
  class A does Callable {
      submethod CALL-ME(|c){ 'called' }
  }
  my &a = A;
  say a(); # OUTPUT: «called␤»
  ```
  
  Applying the `Callable` role is not a requirement to make an object callable; if a class simply wants to add subroutine-like semantics in a regular scalar container, the submethod `CALL-ME` can be used for that.
  
  ```raku
  class A {
      has @.values;
      submethod CALL-ME(Int $x where 0 <= * < @!values.elems) {
          @!values[$x]
      }
  }
  my $a = A.new: values => [4,5,6,7];
  say $a(2); # OUTPUT: «6␤»
  ```
  
  ## [method Capture](#role_Callable "go to top of document")[§](#method_Capture "direct link")
  
  ```raku
  method Capture()
  ```
  
  Throws [`X::Cannot::Capture`](/type/X/Cannot/Capture).
  
  # [Typegraph](# "go to top of document")[§](#typegraphrelations "direct link")
  
  Type relations for `Callable`
  
  
  
  raku-type-graph
  
  
  
  Callable
  Callable
  
  
  Mu
  Mu
  
  
  Any
  Any
  
  
  Any->Mu
  
  
  
  Code
  Code
  
  
  Code->Callable
  
  
  
  Code->Any
  
  
  
  ForeignCode
  ForeignCode
  
  
  ForeignCode->Callable
  
  
  
  ForeignCode->Any
  
  
  
  Block
  Block
  
  
  Block->Code
  
  
  
  WhateverCode
  WhateverCode
  
  
  WhateverCode->Code
  
  
  
  Routine
  Routine
  
  
  Routine->Block
  
  
  
  Sub
  Sub
  
  
  Sub->Routine
  
  
  
  Method
  Method
  
  
  Method->Routine
  
  
  
  Macro
  Macro
  
  
  Macro->Routine
  
  
  
  Submethod
  Submethod
  
  
  Submethod->Routine
  
  
  
  Regex
  Regex
  
  
  Regex->Method
  
  [Expand chart above](/assets/typegraphs/Callable.svg)
  