8

While reading and trying signature smartmatching I run into something strange.

Executing the following smartmaching signature pairs:

my @sigs = :($a, $b), :($a, @b), :($a, %b);
my @signatures_to_check = :($, $), :($, @), :($, %);

my $c = 0;

for @sigs -> $sig {
    for @signatures_to_check -> $s {
        $c++;

        if $sig ~~ $s {
            say "  [ $c ]  " ~ $sig.gist ~ '        match ' ~ $s.gist;
            next;
        }

        say "  [ $c ]  " ~ $sig.gist ~ ' do NOT match ' ~ $s.gist;
    }

    say "\n" ~ '#' x 40 ~ "\n";
}

I've got the following results:

  [ 1 ]  ($a, $b)        match ($, $)
  [ 2 ]  ($a, $b) do NOT match ($, @)
  [ 3 ]  ($a, $b) do NOT match ($, %)

########################################

  [ 4 ]  ($a, @b)        match ($, $)
  [ 5 ]  ($a, @b)        match ($, @)
  [ 6 ]  ($a, @b) do NOT match ($, %)

########################################

  [ 7 ]  ($a, %b)        match ($, $)
  [ 8 ]  ($a, %b) do NOT match ($, @)
  [ 9 ]  ($a, %b)        match ($, %)

I've tried explaining myself cases [ 4 ] and [ 7 ] but I've failed!

Can somebody explain it to me?

jakar
  • 1,701
  • 5
  • 14

1 Answers1

9

How many things is a value that does the Positional role? Or one that does the Associative role?

The hint is in "a value that does..." and "one that does...". It's a single thing.

So, yes, an given Array or Hash has zero, one, two, or more elements. But it is, as itself, a single thing.

$ indicates a scalar symbol or value. What is the constraint on a scalar symbol or value? It is that it binds to a single thing at a time (even if that thing itself can contain multiple elements).

raiph
  • 31,607
  • 3
  • 62
  • 111
  • In that perspective what about cases [ 2 ] and [ 3 ] ? – jakar Jan 02 '20 at 13:11
  • Please look at your Q again, think about it, and if you're still wondering, comment here to ping me to elaborate on 2 and 3. Thanks. – raiph Jun 04 '20 at 05:15