txt: ## variable.md
  class Variable
  
  Object representation of a variable for use in traits
  
  ```raku
  class Variable {}
  ```
  
  Variables have a wealth of compile-time information, but at runtime, accesses to a variable usually act on the value stored inside it, not the variable itself. The runtime class of a variable is [`Scalar`](/type/Scalar).
  
  Class `Variable` holds the compile-time information that traits can use to introspect and manipulate variables.
  
  # [Traits](#class_Variable "go to top of document")[§](#Traits "direct link")
  
  ## [trait is default](#class_Variable "go to top of document")[§](#trait_is_default "direct link")
  
  Sets the default value with which a variable is initialized, and to which it is reset when [`Nil`](/type/Nil) is assigned to it. Trait arguments are evaluated at compile time. Closures won't do what you expect: they are stored as is and need to be called by hand.
  
  ```raku
  my Int $x is default(42);
  say $x;     # OUTPUT: «42␤»
  $x = 5;
  say $x;     # OUTPUT: «5␤»
  # explicit reset:
  $x = Nil;
  say $x;     # OUTPUT: «42␤»
  ```
  
  The trait `is default` can be used also with subscripting things like arrays and hashes:
  
  ```raku
  my @array is default( 'N/A' );
  @array[22].say;  # OUTPUT: N/A
  @array = Nil;
  @array.say;      # OUTPUT: [N/A]
  @array[4].say;   # OUTPUT: N/A
  
  my %hash is default( 'no-value-here' );
  %hash<non-existent-key>.say; # OUTPUT: no-value-here
  %hash<foo> = 'bar';
  %hash<>.say;                 # OUTPUT: {foo => bar}
  %hash<wrong-key>.say;        # OUTPUT: no-value-here
  
  ```
  
  ## [trait is dynamic](#class_Variable "go to top of document")[§](#trait_is_dynamic "direct link")
  
  ```raku
  multi trait_mod:<is>(Variable:D, :$dynamic)
  ```
  
  Marks a variable as dynamic, that is, accessible from inner dynamic scopes without being in an inner lexical scope.
  
  ```raku
  sub introspect() {
      say $CALLER::x;
  }
  my $x is dynamic = 23;
  introspect;         # OUTPUT: «23␤»
  {
      # not dynamic
      my $x;
      introspect()    # dies with an exception of X::Caller::NotDynamic
  }
  
  ```
  
  The `is dynamic` trait is a rather cumbersome way of creating and accessing dynamic variables. A much easier way is to use the `* twigil`:
  
  ```raku
  sub introspect() {
      say $*x;
  }
  my $*x = 23;
  introspect;         # OUTPUT: «23␤»
  {
      # not dynamic
      my $x;
      introspect()    # dies with an exception of X::Dynamic::NotFound
  }
  
  ```
  
  ## [trait of](#class_Variable "go to top of document")[§](#trait_of "direct link")
  
  ```raku
  multi trait_mod:<of>(Mu:U $target, Mu:U $type)
  ```
  
  Sets the type constraint of a container bound to a variable.
  
  ```raku
  my $i of Int = 42;
  $i = "forty plus two";
  CATCH { default { say .^name, ' ', .Str } }
  # OUTPUT: «X::TypeCheck::Assignment Type check failed in assignment to $i; expected Int but got Str ("forty plus two")␤»
  ```
  
  You can use any value defined in compile time as a type constraint, including constants:
  
  ```raku
  constant \T = Int;
  my $i of T = 42;
  ```
  
  which would be equivalent to the previous definition.
  
  # [Methods](#class_Variable "go to top of document")[§](#Methods "direct link")
  
  ## [method name](#class_Variable "go to top of document")[§](#method_name "direct link")
  
  ```raku
  method name(Variable:D: str)
  ```
  
  Returns the name of the variable, including the sigil.
  
  # [Typegraph](# "go to top of document")[§](#typegraphrelations "direct link")
  
  Type relations for `Variable`
  
  
  
  raku-type-graph
  
  
  
  Variable
  Variable
  
  
  Any
  Any
  
  
  Variable->Any
  
  
  
  Mu
  Mu
  
  
  Any->Mu
  
  [Expand chart above](/assets/typegraphs/Variable.svg)
  