class Variable

Object representation of a variable for use in traits

class Variable {}

Variables have a wealth of compile-time information, but at runtime, accesses to a variable usually act on the value stored inside the variable, 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.


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 sub 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 type X::Caller::NotDynamic

trait of

multi sub 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.


method name

method name(Variable:D: str)

Returns the name of the variable, including the sigil.

Type Graph

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

Stand-alone image: vector