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.


Reply via email to