Built-in routines

Coercions

Full

Convert between types with Int/Str/Num/Bool, the prefix + ~ ? operators, and .Type methods.

Raku converts between types explicitly. Each core type doubles as a coercer (Int(...)), each has a prefix operator (+ numeric, ~ string, ? Boolean), and each value has .Int/.Str/.Num/.Bool methods — three spellings of the same conversions.

Type-named coercers #

say Int("42");
say Str(42);
say Num("3.5");
Output
42
42
3.5

Prefix coercion operators #

+ forces numeric context, ~ string context, ? Boolean context.

say +"3.5";
say ~42;
say ?0;
say ?5;
Output
3.5
42
False
True

?0 is False and ?5 is True — the Boolean view of a number is its truthiness (zero is false).

Coercion methods #

The .Type methods do the same conversions, and some carry extra arguments — .base renders an integer in another radix.

say 3.14.Int;
say (1/2).Num;
say 255.base(16);
Output
3
0.5
FF

Notes #