We need to take a few steps back if we want to understand what is happening with your examples:
Variables are tied to objects. By default, sig nal variables are initially bound to assignable container objects ( Scalar for $ and & , Array for @ and Hash for % ).
If you add a type to a variable declaration, for example
my Int %var
or equivalent
my %var of Int
you place a restriction on the types of values ββthis container may contain.
The assignment variable ( = ) tries to put the value on the right side into a container bound to the variable on the left side, which will fail if this type restriction is not met. By default, only & variables have this limitation (cf (my &).VAR.of vs (my %).VAR.of ).
In contrast, reordering a variable ( := ) will replace the container object. If you want to set restrictions on what types of objects can be connected, you need is instead if of :
my %var is Pair; %var := x => 1; # ok %var := 42; # not ok
Sigen variables imply default type restrictions (not for $ , Callable for & , Positional for @ and Associative for % ). Note that this default restriction is overridden explicitly, for example
my %var is Int; %var := 42; # ok even though 42 is not Associative
Finally, note that is not only sets a type restriction, but also binds the variable to a newly created instance of this type:
my %pair is Pair; say %pair;
I do not know how easy it is to make the first one.
Christoph
source share