Subroutines & signatures

Destructuring parameters

Full

Unpack a list or structure directly in the signature with a sub-signature.

A parameter can be a sub-signature in parentheses, which unpacks the incoming structure into named pieces. It works in sub signatures and in pointy blocks, so for loops can destructure each element.

Unpacking a pair of coordinates #

sub dist(($x, $y)) {
    sqrt($x*$x + $y*$y);
}
say dist((3, 4));
Output
5

The single argument (3, 4) is destructured into $x and $y inside the signature.

Destructuring in a for loop #

-> ($a, $b) { … } unpacks each element the loop hands it.

for (1, 2), (3, 4) -> ($a, $b) {
    say $a + $b;
}
Output
3
7

Notes #