develooper Front page | perl.perl6.language | Postings from March 2005

Re: Referencing a caller's slurpy array.

Thread Previous | Thread Next
Larry Wall
March 17, 2005 00:29
Re: Referencing a caller's slurpy array.
Message ID:
On Wed, Mar 16, 2005 at 11:49:12PM -0600, Rod Adams wrote:
: I haven't gotten a solid answer on when and how Perl will autogenerate 
: methods from subs. 

In general I don't think of it as autogeneration at all, but as
failover to a different dispatcher.  I can't think of a case where an
ordinary sub implies an ordinary method or an ordinary method implies
an ordinary sub.  Multi subs can live in both worlds simultaneously
when they're defined within a class, but in that case it's a single
definition that is visible to both dispatchers.  There may end up being
some hanky-panky of an autogeneration nature going on to optimize the
more common failover cases, and we've cut ourselves a little slack in
how we handle single argument functions and/or argumentless methods,
but by and large we should just be able to get away with a single
MMD definition for most purposes, and separate definitions where the
valence actually changes.

: For instance, would
:  @array.grep(/foo/);
: generate a call to the list op C<grep> above, or do I need to be 
: defining another form of C<grep> that's more agreeable to 
: autogeneration? If so, what makes a sub more agreeable to auto method 
: generation?

Well, grep is one of those cases where you really need two different
definitions because you have conflicting invocants.  If within the
Array class you write a multi sub like this:

    multi sub grep (Array @array, Selector $for) {...}

then it can be called either as @array.grep(/foo/) or
grep(@array,/foo/), but never as grep(/foo/,@array).

At least, not unless we install some mechanism like active/passive
voice into Perl, in which case the passive voice could be autogenerated
from the active voice, albeit with a modified verb.  But for now
I think two definitions are appropriate, with some thought about
bifurcating the verb into "do to" and "done by" variants if there's
likely to be MMD confusion.  The .grep variant should probably just
be an ordinary method rather than a "squinting" multi method to avoid
confusion with the standard listop form.

If we find ourselves with double declarations all over the place, then
maybe we can look for some autogeneration relief.


Thread Previous | Thread Next Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at | Group listing | About