Multiple dispatch (Re: Q: Python 2.0 preliminary features?)

Louis Madon madonl at bigfoot.com
Thu Oct 28 00:39:06 EDT 1999


Greg Ewing wrote:
> 
> Multiple dispatch is an attractive idea, but it makes me uneasy.
> One reason is that it's not clear what the rules should be.
> 
> 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?

Neither, you should get a "message ambiguous" error unless (as you say
below) some arbitrary rule is used to disambiguate.    

> 
> Arbitrary rules can of course be invented, but there is
> no single choice that is obviously "right" the way there
> is with single dispatch.

Yes, but why is having a "single choice" so important?  There is an
analogy here with static vs. dynamic typing.  In a statically typed
language there is always a well defined (ie. single choice) type for
each variable so you know you'll never get a type error at runtime.  Not
so with dynamic typing, nonetheless many people prefer that for numerous
reasons (eg. greater flexibility), and argue that type errors can be
dealt with during testing.  Similarly, multiple dispatch gives you a
more expressive object system - thus permitting simpler, more reusable
class models in many cases - so dealing with the occasional ambiguous
message error during testing seems a small price to pay. 

> 
> Multiple inheritance has a similar problem, which is why
> I'm not all that keen on it either, unless used in a very
> restricted way.

But if the domain you need to model isn't naturally hierarchical you'll
end up with redundancy in your class model making the projects'
evolution and maintenance more tedious.  No MI is Ok if the project is
relatively small, but as you scale things up the redundancy problem just
keeps getting worse.  [Admittedly, MI is not implemented to well in some
languages, eg, c++, but thats no reason to throw the baby out with the
bath water].

> 
> If you have multiple dispatch *and* multiple inheritance,
> things get just too hairy to contemplate. I tried to devise
> a set of rules for this case once, but my brain got dangerously
> close to exploding and I had to give up...

Can you elaborate? I mean about MD + MI (Its ok, I'll keep a safe
distance from you so feel free to self destruct while considering
this :-)

Louis




More information about the Python-list mailing list