[Prévia] [Próxima] [Prévia por assunto] [Próxima por assunto]
[Índice cronológico] [Índice de assunto]

FWD: Double  Dispatch



                                                                                                 
                                                                                                 
                                                                                                 
                                                                                                 
 De: Andrew P. Black                                                                             
 Para: Squeak Mailing List                                                                       
 Data: 25/02/2003 22:47                                                                          
 Assunto: Double Dispatch                                                                        
                                                                                                 
                                                                                                 



                                                                                                 
                                                                                                 
                                                                                                 
                                                                                                 
 This is a call for language historians!                                                         
                                                                                                 
 In teaching OOP, I often ask students to read and discuss Dan                                   
 Ingall's 1986 OOPSLA paper.  It's short, clear, and provides an                                 
 elegant and efficient solution to a well-described problem.                                     
                                                                                                 
 The problem is how to simulate "multiple dispatch".  You have three                             
 kinds  of graphical objects -- rectangles, ovals and bitmaps -- and                             
 three kinds of display ports -- canvas, printer and remote -- and so                            
 you need nine different displayOn: methods.  How should one implement                           
 this cleanly in a "single dispatch" language like Smalltalk, i.e., in                           
 a language in which the method chosen depends on the message and on                             
 the class of the target object, but not on the class of the arguments?                          
                                                                                                 
 Dan argued, quite correctly, that it is a BAD IDEA to do explicit                               
 test of class membership                                                                        
                                                                                                 
 rectangle>>drawOn: aDisplayPort                                                                 
 (aDisplayPort isKindOf: canvas) ifTrue:                                                         
 [ ... code to draw a rectangle on a canvas ...]                                                 
 (aDisplayPort isKindOf: printer) ifTrue:                                                        
 [ ... code to draw a rectangle on a printer ...]                                                
 ...                                                                                             
                                                                                                 
 because this code must be re-written each time a new kind of                                    
 DisplayPort is added to the system.  Instead we should do a second                              
 dispatch, to get the effect of the case statement:                                              
                                                                                                 
 rectangle>>drawOn: aDisplayPort                                                                 
 aDisplayPort displayRectangle: self                                                             
                                                                                                 
                                                                                                 
 canvas>>displayRectangle: aRectangle                                                            
  ... code to draw a rectangle on a canvas ...                                                   
                                                                                                 
 printer>>displayRectangle: aRectangle                                                           
  ... code to draw a rectangle on a printer ...                                                  
                                                                                                 
 This is extensible and modular, and also efficient.  It is used in                              
 more complex patterns like the visitor pattern, and is fairly well                              
 know to the readers of this list, I am sure.                                                    
                                                                                                 
 As far as I am aware, this technique is called Double Dispatch.                                 
 Several of my Smalltalk books call it Double Dispatch, for example,                             
 LaLonde and Pugh use this name is discussing the various                                        
 implementations of arithmetic in the number hierarchy, which now this                           
 technique in preference to the "genericity numbers" used in earlier                             
 Smalltalks.  Skublics, Klimas and Thomas, in "Smalltalk with Style"                             
 also use the term Double Dispatch for this technique.                                           
                                                                                                 
 My question is: who first introduced the term "Double Dispatch" to                              
 refer to this technique?  I was amazed to find that this term is                                
 *not* used in Dan Ingall's original paper, although it is a perfectly                           
 natural extension of Dan's Language.  I'm asking because I recently                             
 came across a paper that defined "Double Dispatch" to mean the                                  
 deprecated style of explicitly testing the class of the argument,                               
 which (as far as I'm concerned) is exactly what Double Dispatch is                              
 NOT.