Rakudo (both "star 201503" & rakudo-moar 7b5256) complains about "anon
multi", saying "Cannot use 'anon' with individual multi candidates. Please
declare an anon-scoped proto instead" in response to "anon multi foo (Int
$x) { $x + 1 };"

Given that message, I created an "anon proto foo" and assigned it to a
scalar:

perl6 -e "my $y=anon proto foo (|) {*}; multi foo (Int $x) { $x + 1 };  say
$y.name; say $y(7)"

which says foo, showing that the anonymous dispatcher does know its name,
and then fails with "Cannot call foo(Int); none of these signatures match:"
- no signatures listed.

My understanding of "anon" is that it prevents the following declaration
from being installed into any namespace, with only the object storing its
name. What seems to happen, is that the following "multi foo" creates a new
dispatcher, because it can't see the "anon proto foo." The the existing
anonymous dispatcher can't ever have any candidates.

Perl6 IRC's bot tells me that "std" accepts "anon multi" declarations
without complaint. That, plus the not-so-useful behavior of "anon proto"
makes me wonder what the intent is with "anon proto/multi." Three thoughts
come to me:

1. An "anon proto" is useful in some way I don't know. Maybe it's good for
run-time dispatcher twiddling eg. "my $int_inc = sub (Int $x) { $x + 1
}; $dispatcher=anon
proto Inc-Anon (Num $z) {*}; $dispatcher.add-multi($int_inc); say 'Perl ',
$dispatcher(5);"

2. Rakudo currently seems to implement multi-subs with each "multi"
declaration looking for a "proto" to hang on to at compilation, creating
one in the same scope if none are found. If that's the case, perhaps "anon
proto" should be illegal, and "anon multi" would hide that multi signature
& sub from everything except its proto- which would know because the when
the anon-multi-sub was first created, it installed itself into its
(non-anon-)proto.

3. If the responsibility of binding "multi" to "proto" was in "proto" and
delayed until runtime, then an "$a = anon proto foo" could indeed find any
"multi foo" declared in the same scope. Drawback is potential for confusion
if anything calls "foo" directly, it would create a new dispatcher.

My speculation on implementation is haphazard and not well informed. What's
the *intent *of "anon proto" and perhaps "anon multi" subs? Std and Rakudo
have different ideas of what is legal, and the current implementation hints
at untapped potential.

-y

Reply via email to