http://qs1969.pair.com?node_id=343146


in reply to Why breaking can() is acceptable

Interesting discussion. Perl 6 will not need to encourage people to define their own .can. Here's a paragraph from A12:
By the way, unlike in Perl 5 where .can returns a single routine reference, Perl 6's version of .meta.can returns a "WALK" iterator for a set of routines that match the name. When dereferenced, the iterator gets fed to a dispatcher as if the method had been called in the first place. Note that any wildcard methods (via delegation or AUTOLOAD) are included in this list of potential handlers, so there is little reason for subclasses to have to redefine .can to reflect the new names. It does weaken the meaning of .can from "definitely has a method of this name" to "definitely has one or more methods in one or more classes that will try to handle this." But that's probably closer to what you want, and the best we can do when people start fooling around with wildcard methods under MI.
Also, we're making a clean separation between AUTOLOAD, which does "wildcard" methods, and AUTODEF, which may only supply a definition for predeclared (stubbed) methods or subroutines. (The delegation syntax also makes a clear distinction between those methods we know the name of in advance and those we don't.)

Replies are listed 'Best First'.
Re: Re: Why breaking can() is acceptable
by tilly (Archbishop) on Apr 06, 2004 at 22:53 UTC
    I'm sure that I don't really understand the way that you described that, likely because I have not read the rest of A12 yet.

    However if you haven't nailed that part of the specification down, this discussion has convinced me that it would be a definite improvement to have a wildcard decision on whether to handle a method. That is, instead of writing something like an AUTOLOAD, you would write a method named something like CAN, which would dynamically decide whether or not to accept this method, and if it does, returns the subroutine that does it.

    In thinking about it, I've had trouble coming up with anything that you'd want to do with a reasonable AUTOLOAD which cannot readily be duplicated by a CAN that works like this. Furthermore while it is very hard to make multiple inheritance and AUTOLOAD cooperate, this CAN strategy cooperates with multiple inheritance rather smoothly. In fact it returns the best possible for can() to "definitely has a method of this name", even in the face of multiple inheritance and wildcard methods.

    Unfortunately if a user wanted to implement this in Perl 6, as I understand your paragraph they would not be able to pass the extra information to can() that, "I can really tell you whether I'll do that". Which prevents can() from giving the answer that we probably really wanted it to give.

    In fact I'm seriously thinking of writing a module named something like UNIVERSAL::AUTOLOAD_CAN which implements a UNIVERSAL::AUTOLOAD and UNIVERSAL::can that implements this solution in Perl 5. This would make it possible to get can(), AUTOLOAD and multiple inheritance to play together. (But only if you write no other AUTOLOADs!)

      I think this will work out ok if .meta.can, when it's looking for wildcarding classes, distinguishes classes that define their own .can from those that don't, and only adds autoloaders to the end of the list that aren't shielded by a corresponding .can method. (The problems Perl 5 has with defining a .can method in a class shouldn't arise in Perl 6, which tries to prefer "next" semantics to "super" semantics. At worst we'll require people to say "next METHOD" at the end of each such .can method definition.)
        If I make a few assumptions about how .meta.can works, that should work out. (As long as you're aware of the issue and want it to work out, I'm sure that you can fulfill some set of conditions to make things work out.)

        I'm guessing that the programmer would still need to synchronize .can and AUTOLOAD in some way, but a mechanism to make that easy should be simple to add at the user level.

Re: Re: Why breaking can() is acceptable
by runrig (Abbot) on Apr 07, 2004 at 00:44 UTC
    If a package/class has an AUTOLOAD sub, will you be able to decline to handle a method, but say "I don't want to handle this, but keep looking in the inheritance tree"? Say for some reason, you want your class to handle get_* methods, and the next class in ISA (or whatever the Perl 6 equivalent is), to handle set_* methods; nevermind how good/bad of a design this is :)
      Yes, you can always call "next METHOD" to pass the buck to the next method in the list of candidates. Though an AUTOLOAD is going to be pretty far down the pecking order, because Perl will prefer all declared methods over all wildcard methods, and autoloading is one form of wildcarding. So only an AUTOLOAD in a parent class (or a sibling class, since these are "next" semantics) is possible in the remaining list of candidates.

      Though as for get_* and set_* methods, those would be non-standard in Perl 6. Perl's accessor methods will not be split into get and set methods. Rather, they'll be single methods that are lvaluable if you want to allow setting.

Re: Re: Why breaking can() is acceptable
by duff (Parson) on Apr 07, 2004 at 00:56 UTC
    Here's a paragraph from A12:

    And when will be seeing the rest of A12? ;-)