Documentation for method default
assembled from the following pages:
Role: Baggy §
From Baggy
(Baggy) method default §
Defined as:
method default(Baggy: --> 0)
Returns zero.
my = bag <eggs bacon>;say .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 = SetHash.new(1, 2, 3);say ; # OUTPUT: «True» = Nil;say ; # OUTPUT: «False» # access non initialized element say ; # OUTPUT: «False»
Class: Hash §
From Hash
(Hash) method default §
Defined as:
method default(Hash:)
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 = 'apples' => 3, 'oranges' => 7;say .default; # OUTPUT: «(Any)» say ; # OUTPUT: «(Any)» my is default(1) = 'apples' => 3, 'oranges' => 7;say .default; # OUTPUT: «1» say + ; # OUTPUT: «4»
Class: Scalar §
From Scalar
(Scalar) method default §
method default(Scalar: --> Str)
Returns the default value associated with the container.
Example:
my is default(666) = 42;say .VAR.default; # OUTPUT: «666»
Class: Parameter §
From Parameter
(Parameter) method default §
Defined as:
method default(Parameter: --> 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 = :(, = 12);say .params[0].default; # OUTPUT: «(Code)» say .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 = 1, "two", 2.718;say .default; # OUTPUT: «(Any)» say [4]; # OUTPUT: «(Any)» my is default(17) = 1, "two", 3;say .default; # OUTPUT: «17» say [4]; # OUTPUT: «17» [1] = Nil; # (resets element to its default) say [1]; # OUTPUT: «17»