Course of Raku / Advanced / Containers / Scalar containers / Exercises / Integers only
Solution: Integers only
Here is a possible solution to the task.
Code
my Int $cars;
$cars = 250.7.Int;
say $cars;
say $cars.WHAT;🦋 You can find the source code in the file integers-only.raku.
Output
250
(Int)Comments
The
Inttype constraint inmy Int $carsmakes the container reject any value that is not an integer. A direct assignment of250.7(aRat) would be a compile-time error.Calling
.Inton the rational value250.7converts it to the integer250by dropping the fractional part. The converted value fits the container. Make sure to not mix this behaviour with rounding.The
.WHATpseudo-method confirms that the value stored in the container is of typeInt.