Course of Raku / Advanced / More about built-in types / Native types 🆕 / Exercises / The native default

Solution: The native default

Here is a possible solution to the task.

Code

my num $n;
my str $s;

say $n;
say "[$s]";

🦋 You can find the source code in the file native-default.raku.

Output

0
[]

Comments

  1. A native num cannot be undefined, so like a native int it starts at 0 rather than at (Num).

  2. A native str starts as the empty string, which is why the brackets come out with nothing between them. None of the native types ever hold an undefined value.

  3. The type specifiers are essential here. Drop them — write my $n; my $s; — and the variables become ordinary containers that start out undefined (Any). Then say $n prints (Any), and interpolating the undefined $s warns “Use of uninitialized value … in string context”. It is the native num and str types that guarantee the 0 and empty-string defaults.

Course navigation

The native default   |   Wrapping around