From: jot@sps.com (Joe Tallet)
Newsgroups: comp.lang.ada
Subject: Ada 9x Dispatching Question
Keywords: Ada Dispatching Polymorphism
Date: 13 Aug 92 15:10:10 GMT
Organization: Software Productivity Solutions, Inc (SPS), Melbourne, FL, USA


I have a question concerning the code within Alex's article.

Alex defined a class hierarchy:
  Shape
    Circle

Here are the highlites of his code:

>package Shapes is
>
>  type shape is tagged ...;
>
>  procedure draw (x : shape) is <>; -- abstract
>
>end Shapes;
>
>----------------
>with Shapes;
>package Circles is
>
>  type circle is new Shapes.shape ...;  
>   -- inherits primative ops of shape
>
>  procedure draw (x : circle); 
>   -- overide draw, now circle is not abstract
>
>end Circle;
>
>----------------
>with Shapes;
>procedure client (x : shapes.shape) is
>begin
>  -- do something & then call the correct draw procedure
>  -- for the actual parameter x.
>  shapes.draw (x); -- dispatches      <<<<<<<<<<<<<<<<<<<-+
>end client;                                               |
>                                                          |
>---------------------------------------------------       |
>Alex Blakemore alex@cs.umd.edu   NeXT mail accepted       |
>                                                          |
                                                           |
                                                           |
  +--------------------------------------------------------+
  |
  +--- Is this True?

  If I call Client and pass in a Circle object, will Shapes.Draw 
dispatch to Circle.Draw!?!  Unless I am mistaken the answer is NO!

My understanding of Ada 9x's dispatching is this:

  To aid in this example I'll add a two more classes...
 
---
with Shapes;
package Squares is
  type Square is new Shapes.Shape ...;
  procedure Draw (X : Square);
end Squares;

---
with Shapes;
package Triangles is
  type Triangle is new Shapes.Shape ...;
  procedure Draw (X : Triangle);
end Triangles;


  Now I'll modify the Client routine to reference these classes.  

---
with Shapes, Circles, Squares, Triangles;
USE Circles, Squares, Triangles;  
-- USE Shapes;  I don't think I need this.
procedure Client (X : Shapes.Shape) is
begin
  -- Real code goes here . . .
  draw (x); -- 'dispatches' because of the USE.
end Client;

My understanding of the dispatching is that the USE clause
is necessary.  Without it, we would need:

case <object type> is
  when <circle>   => Circle.Draw (X);
  when <square>   => Square.Draw (X);
  when <triangle> => Triangle.Draw (X);
  when others => null;
end case;


My question is, do you call the common ancestor for 
message dispatching, or do you use USE?

Thanks in advance for responses,

jot
--====================================================================--
Joe Tallet                   jot@sps.com                  !uunet!sps!jot
Software Productivity Solutions, Inc.
122 Fourth Ave.
Indialantic, FL 32903                                       407-984-3370
--====================================================================--
     M4&@M;F=L=6D@;6=L=R=N869H($-T:'5L:'4@4B=L>65H('=G86@G;F%G;"!F
     ':'1A9VXN"FD@
--====================================================================--