Documentation for method default assembled from the following pages:

Role: Baggy §

From Baggy

(Baggy) method default §

Defined as:

method default(Baggy:D: --> 0)

Returns zero.

my $breakfast = bag <eggs bacon>;
say $breakfast.default;                           # OUTPUT: «0␤»

Role: Setty §

From Setty

(Setty) method default §

Defined as:

method default(--> False)

Returns the default value of the invocant, i.e. the value which is returned when trying to access an element in the Setty object which has not been previously initialized or when accessing an element which has explicitly been set to Nil or False.

my $s1 = SetHash.new(123);
say $s1{2};                                           # OUTPUT: «True␤» 
$s1{2} = Nil;
say $s1{2};                                           # OUTPUT: «False␤» 
# access non initialized element 
say $s1{4};                                           # OUTPUT: «False␤»

Class: Hash §

From Hash

(Hash) method default §

Defined as:

method default(Hash:D:)

Returns the default value of the invocant, i.e. the value which is returned when a non existing key is used to access an element in the Hash. Unless the Hash is declared as having a default value by using the is default trait the method returns the type object (Any).

my %h1 = 'apples' => 3'oranges' => 7;
say %h1.default;                                       # OUTPUT: «(Any)␤» 
say %h1{'bananas'};                                    # OUTPUT: «(Any)␤» 
 
my %h2 is default(1= 'apples' => 3'oranges' => 7;
say %h2.default;                                       # OUTPUT: «1␤» 
say %h2{'apples'} + %h2{'bananas'};                    # OUTPUT: «4␤»

Class: Scalar §

From Scalar

(Scalar) method default §

method default(Scalar:D: --> Str)

Returns the default value associated with the container.

Example:

my $x is default(666= 42;
say $x.VAR.default;             # OUTPUT: «666»

Class: Parameter §

From Parameter

(Parameter) method default §

Defined as:

method default(Parameter:D: --> Code:_)

Returns a closure that upon invocation returns the default value for this parameter, or Code if no default was provided.

Note: Before Rakudo version 2020.08 the return value for a parameter with no default value was Any.

my $sig = :($a$b = 12);
say $sig.params[0].default;        # OUTPUT: «(Code)␤» 
say $sig.params[1].default.();     # OUTPUT: «12␤» 

Class: Array §

From Array

(Array) method default §

Defined as:

method default

Returns the default value of the invocant, i.e. the value which is returned when trying to access an element in the Array which has not been previously initialized or when accessing an element which has explicitly been set to Nil. Unless the Array is declared as having a default value by using the is default trait the method returns the type object (Any).

my @a1 = 1"two"2.718;
say @a1.default;                               # OUTPUT: «(Any)␤» 
say @a1[4];                                    # OUTPUT: «(Any)␤» 
 
my @a2 is default(17= 1"two"3;
say @a2.default;                               # OUTPUT: «17␤» 
say @a2[4];                                    # OUTPUT: «17␤» 
@a2[1= Nil;                                  # (resets element to its default) 
say @a2[1];                                    # OUTPUT: «17␤»