Can I bind multiple variables at once?
Asked Answered
K

1

10

The following line declares a variable and binds it to the number on the right-hand side.

my $a := 42;

The effect is that $a is not a Scalar, but an Int, as can be seen by

say $a.VAR.^name;

My question is, can I bind multiple variables in one declaration? This doesn't work:

my ($a, $b) := 17, 42;

because, as can be seen using say $a.VAR.^name, both $a and $b are Scalars now. (I think I understand why this happens, the question is whether there is a different approach that would bind both $a and $b to given Ints without creating Scalars.)

Furthermore, is there any difference between using := and = in this case?

Kaitlin answered 27/6, 2021 at 15:25 Comment(1)
You bind the left hand side to the right hand side. In the second case, you're binding a list to another. The list elements are assigned, not bound, the elements on the right hand side. Binding is not recursive. Making it so would probably involve using map or somesuch, but not the assignment statement itself. And no, there's no difference.Piscatory
P
9

You can use sigilless containers:

my (\a, \b) := 17,42;
say a.VAR.^name; # Int

Sigilless variables do not have an associated container

Piscatory answered 27/6, 2021 at 16:13 Comment(2)
Interestingly, if I use my (\a, \b) = 17, 42 (assignment, not binding), then say a.VAR.^name prints Scalar. What happens there?Kaitlin
[:=] my \a, my \b, 17 #Cannot reduce with := because list assignment operators are too fiddlyCatalano

© 2022 - 2024 — McMap. All rights reserved.