Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Signatures smartmatching misunderstanding

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?

like image 275
jakar Avatar asked Jan 02 '20 12:01

jakar


1 Answers

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).

like image 190
raiph Avatar answered Oct 24 '22 22:10

raiph