Newsgroups: comp.lang.dylan
Path: cantaloupe.srv.cs.cmu.edu!bb3.andrew.cmu.edu!newsfeed.pitt.edu!gatech!news.mathworks.com!newsfeed.internetmci.com!uwm.edu!fnnews.fnal.gov!cbgw1.att.com!nntphub.cb.lucent.com!alice!allegra.att.com!dujardin
From: dujardin@allegra.att.com (Eric Dujardin)
Subject: Re: methods in clos and dylan
X-Nntp-Posting-Host: ode.tempo.att.com
Message-ID: <Dwur9J.K88@research.bell-labs.com>
Sender: usenet@research.bell-labs.com (netnews <9149-80593> 0112740)
Organization: Lucent Technologies, Bell Laboratories
References: <4v2tee$s4f@newsbf02.news.aol.com> <ymi7mqvien6.fsf@doc.isi.edu> <321D6265.19A7@khe.scn.de> <y8ak9um9lgt.fsf@hertie.artcom.de> <3222DDAF.80B@khe.scn.de>
Date: Wed, 28 Aug 1996 14:36:55 GMT
Lines: 46

In article <3222DDAF.80B@khe.scn.de>, Ulrich Schreiner <schreine@khe.scn.de> writes:
|> Andreas Bogk wrote:
|> > In my opinion most problems can't be solved by single dispatch. Even
|> > the simple problem of computing:
|> > 
|> >  a + b
|> > 
|> > requires multiple dispatch, if the type of a and b are not
|> > equal. Trying to solve that problem with single dispatch would require
|> > arbitrary decisions that decrease code readability.
|> > 
|> 
|> one question: how do you compute >"hello world" + 4.5< ??
|> 
|> i think you'll first convert the number 4.5 to a string and then 
|> concatenate the two strings. so you simply send an instance of the class 
|> string ("hello world") the message "+" with an object as paraemter 
|> (4.5). the "+"-method simply converts this parameter to a string and 
|> concatenates.
|> 


So if method "+" of class string receives a string, it simply concatenates it,
and if it receives a non-string, it first converts it to a string before 
concatenation. That is called double dispatch, it is the classical way of 
expressing multiple dispatch with single dispatch. The problem with double 
dispatch is that it is far less clear, less efficient, and less extensible
than multiple dispatch. 
It's less clear because you need many "convert-to-whatever-supports-addition"
methods to also deal with Integer,Real etc instead of String. More generally,
each time you would need a multiply dispatched generic function m(x,y), 
you will declare methods like
"handle-second-argument-of-m-when-first-arg-is-classXX".
It's less efficient because you pay the price of two invocations,
eg two argument passings, and the compiler deals separately with all these 
methods instead of using more compact global data structures.
It's less extensible because each time you add a class that supports this 
operation, you also have to define the methods to handle all the second args.



-- 
Eric Dujardin                                 dujardin@allegra.lucent.com
Lucent Technologies - Bell Laboratories
Room 2C-234, 600 Mountain Avenue, P.O. Box 636      Phone: (908) 582-7153
Murray Hill, NJ 07974-0636, USA                     Fax  : (908) 582-5809
