Course of Raku / Advanced / Containers / Scalar containers / Exercises / Typed or untyped
Solution: Typed or untyped
Here is a possible solution to the task.
Code
my $untyped;
my Int $typed;
dd $untyped;
dd $typed;
$untyped = 42;
$typed = 42;
dd $untyped;
dd $typed;🦋 You can find the source code in the file reuse-the-container.raku.
Output
$untyped = Any
Int $typed = Int
$untyped = 42
Int $typed = 42Comments
ddreports the two containers differently. For the untyped one it prints just$untyped, while for the typed one it prefixes the declared type:Int $typed. That prefix is exactly the difference a declared type makes.The empty values differ too. An untyped container starts at the bare undefined value
Any, whereas a typed container starts at the undefined value of its type,Int.After both are assigned
42, the values look the same, but$typedstill carries its type and would reject a non-integer value — unlike$untyped, which accepts anything.