Browse files

Un-confusing Signature example

Inverting the order by using $sig in the `given` clause, I expect to
make it a bit less confusing. Also inclusing the `default` clause,
just in case. Moved `mismatch` to the first, to show that it's checked
against. Hope this closes #1777, if it does not, just let me know.
  • Loading branch information...
JJ committed Feb 14, 2018
1 parent e79d237 commit 653a7994d3e18ab0963270c3e06b0ddee57ad5fa
Showing with 5 additions and 4 deletions.
  1. +5 −4 doc/Type/Signature.pod6
@@ -58,10 +58,11 @@ Smart matching signatures against a List is supported.
my $sig = :(Int $i, Str $s);
say (10, 'answer') ~~ $sig;
# OUTPUT: «True␤»
given ('answer', 10) {
when :(Str, Int) { say 'match' }
when $sig { say 'mismatch' }
given $sig {
when ('answer',10) { say 'mismatch' }
when :(Int, Str) { say 'match' }
default { say 'no match' }
# OUTPUT: «match␤»
When smart matching against a Hash, the signature is assumed to consist of the keys of the Hash.

2 comments on commit 653a799


This comment has been minimized.

jimav replied Feb 14, 2018

Still seems confusing because the example would be a bug in real code, and this isn't explained.

The first when clause is a reversed smartmatch which could never ever match for any $sig. The fact that ~~ is not commutative isn't obvious, and confusion (in my mind anyway) is compounded because the example reverses the order of the items in the ('answer',10) list; this is a complete red herring because what's in the list makes no difference.

If the intent is to point out how given / when maps to if/then/else with infix:<~~>, and/or that ~~ is not commutative between Signature and List, then some additional explanations would be helpful [the preceding reflects my so-far limited understanding of P6].

Ok, I'm treading on thin ice here -- is there a signature for which $sig ~~ ("answer",10) (or $sig ~~ (10,'answer')) is ever True? That is the condition tested by the first when clause in the updated example...

Would you mind critiquing the following? It would help me understand the issues.

my $sig = :(Int $i, Str $s);
say (10, 'answer') ~~ $sig; # OUTPUT: «True␤»
say $sig ~~ (10, 'answer'); # OUTPUT: «False␤» [explain briefly why...]
given $sig {
when (10, "answer") { die "never gets here" } # means $sig ~~ (10,'answer') which never matches
when :(Int, Str) { say 'integer then string' }
when :(Str, Int) { say 'string then integer' }
default { say 'no match' }


This comment has been minimized.


JJ replied Feb 15, 2018

Actually, the point is to explain signatures, so maybe your initial example was the correct one. I'll continue discussion there.

Please sign in to comment.