method default

Documentation for method default assembled from the following types:

class Hash

From Hash

(Hash) method default

Defined as:

method default()

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»

role Baggy

From Baggy

(Baggy) method default

Defined as:

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

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 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␤» 

class Parameter

From Parameter

(Parameter) method default

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