Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / August 2007

Tip: Looking for answers? Try searching our database.

dispatching to same function name based on argument type

Thread view: 
metaperl - 24 Aug 2007 15:50 GMT
In this video -
  http://video.google.com/videoplay?docid=448441135356213813&q=common+lisp&total=1
4&start=0&num=10&so=0&type=search&plindex=0


Peter Seibel says that Lisp generic functions give Lisp an edge in
"double dispatch" or even higher degrees of dispatch. I.e., when you
must decide which function to run based on the types of n or more
things.

The example he gave was displaying one of { Book, CD, DVD } to
any of { Text , Graphics }

but can't you simply write a bunch of static functions for each case?
Ie:

static void display(Book b, TextDisplay t);
static void display(Book b, GraphicDisplay t);

and so on?
Patricia Shanahan - 24 Aug 2007 16:06 GMT
> In this video -
>    http://video.google.com/videoplay?docid=448441135356213813&q=common+lisp&total=1
4&start=0&num=10&so=0&type=search&plindex=0

[quoted text clipped - 14 lines]
>
> and so on?

You can, but the class containing the static methods would have N*M
methods, where N is the number of media and M is the number of displays.
It would have to be updated for addition of either a new medium or a new
display type.

Ideally, one would implement e.g. paper tape input by adding a class
PaperTape, implementing the same interface as Book, CD, and DVD, and
that would be the end of the matter.

Patricia
Eric Sosman - 24 Aug 2007 17:49 GMT
Patricia Shanahan wrote On 08/24/07 11:06,:

>>In this video -
>>   http://video.google.com/videoplay?docid=448441135356213813&q=common+lisp&total=1
4&start=0&num=10&so=0&type=search&plindex=0

[quoted text clipped - 23 lines]
> PaperTape, implementing the same interface as Book, CD, and DVD, and
> that would be the end of the matter.

   Also, note that Java's determination of which method
to call depends on the compile-time types of the arguments,
not their run-time types.  If all these things are subclasses
of Media and Display, as appropriate, then

    Media m = new Book();
    Display d = new TextDisplay();
    display(m, d);

... will *not* call the first of your static functions.

Signature

Eric.Sosman@sun.com



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.