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.

Class Variable holds the compile-time information that traits can use to introspect and manipulate variables.

Traits§

trait is default§

Sets the default value with which a variable is initialized, and to which it is reset when 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.

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:

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§

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.

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:

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§

multi trait_mod:<of>(Mu:U $targetMu:U $type)

Sets the type constraint of a container bound to a variable.

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:

constant \T = Int;
my $i of T = 42;

which would be equivalent to the previous definition.

Methods§

method name§

method name(Variable:D: str)

Returns the name of the variable, including the sigil.

Typegraph§

Type relations for Variable
raku-type-graph Variable Variable Any Any Variable->Any Mu Mu Any->Mu

Expand chart above