Built-in routines
Coercions
FullConvert 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.5Prefix 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
FFNotes #
.Inton a fractional value truncates toward zero (3.9.Intis3), it does not round.- Numeric coercion of a bad string is a soft failure (a
Failure), not a crash — guard with//or check.definedwhen parsing untrusted input. - The prefix operators are the idiomatic short forms:
+$x,~$x,?$xread better than the method calls in expressions.