[Prev][Next][Index][Thread]
Re: Multimethods 101 (was Re: Multiple dispatch (Re: Q: Python 2.0 preliminary features?))
In article <3819EA9D.6A46E845@mail.earthlink.net>,
Charles Hixson <charleshixsn@mail.earthlink.net> wrote:
>Louis Madon wrote:
>
>> """ snip """
>> In the beginning Greg Ewing wrote:
>>
>> > Suppose AA is a subclass of A, and BB is a subclass of B,
>> > and there are methods defined for the combinations
>> > foo(A, B), foo(AA, B) and foo(A, BB).
>> >
>> > Now, if you make a call with the combination foo(AA, BB),
>> > which method should get called?
>>
>> Several people said its "ambiguous".
>
>It's ambiguous unless you define what should happen. To my mind the
>apporpriate call would be to foo (A,B), but any one of them would be a valid
>interpretation in the absence of language definitions. Depending on search
>order, it would seem most Pythonish to call the foo (AA, B) method, since
>that would probably be the first one found along the left hand branch of the
>search path, and it does fit.
I think CLOS would also call that one by default. It allows argument
priority order to be declared in the generic function declaration, so you
could make it select either foo(AA, b) or foo(A, BB). Left-to-right is the
default.
One of the reasons why I think this hasn't been too much of a problem is
CLOS's :before and :after method types. When composing classes, it's often
more common to add these secondary method types rather than to override the
primary method. So you'll have a primary foo(A, B), and secondary foo(AA,
B) and foo(A, BB) methods that add their own functionality to the primary
method. All of them will be called automatically.
--
Barry Margolin, barmar@bbnplanet.com
GTE Internetworking, Powered by BBN, Burlington, MA
*** DON'T SEND TECHNICAL QUESTIONS DIRECTLY TO ME, post them to newsgroups.
Please DON'T copy followups to me -- I'll assume it wasn't posted to the group.
References: