trait is default (Variable)

Documentation for trait is default (Variable), assembled from the following types:

class Variable

From Variable

(Variable) trait is default (Variable)

multi sub trait_mod:<is>(Variable:D:$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