Course of Raku / Advanced / Containers / Scalar containers
Default values
When a new scalar variable is created without an immediate assignment, the container still holds a value — its default value. The exact default depends on the type of the variable.
The program below is not the best way to use Raku, but it shows the point:
my $int;
say $int + 5;The output contains 5, which may be what you expected if
you assumed that the default value of $int is
0. However, the program also prints a warning:
Use of uninitialized value $int of type Any in numeric context
in block <unit> at t.raku line 2
5To remove this uncertainty, either assign a value explicitly:
my $int = 0;
say $int + 5; # 5Or declare a default value with the is default
trait:
my $int is default(0);
say $int + 5; # 5The default value is not limited to zero. It can be any value that you consider a good candidate, for example:
my $int is default(1);
say $int + 5; # 6Course navigation
← Quiz — Type constraints | Proxying method calls →
💪 Or jump directly to the exercises in this
section.