Converting types using type coercion methods
To convert, or to coerce a type, call a method with the data type’s name. For example, convert a string into an integer number:
my $howmany = '42'; my $n = $howmany.Int; say $n;
In many cases, you don’t need to convert values explicitly, as Raku does that job for you. The following code is working correctly and predictably:
my $a = '10'; my $b = '20'; my $c = $a + $b; say "The result is $c."; # The result is 30.
The two variables,
$b, contain strings, but they are used in an arithmetical expression with
+. At this point, both strings are converted to integers, and thus
$c also gets an integer. A variable containing an integer is interpolated in a string in the last line, where Raku does the necessary operations to present the number as a sequence of digit characters.
💪 Or jump directly to the exercises to this section.