home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #18 / NN_1992_18.iso / spool / comp / lang / ada / 2354 < prev    next >
Encoding:
Internet Message Format  |  1992-08-13  |  3.2 KB

  1. Path: sparky!uunet!sps!jot
  2. From: jot@sps.com (Joe Tallet)
  3. Newsgroups: comp.lang.ada
  4. Subject: Ada 9x Dispatching Question
  5. Keywords: Ada Dispatching Polymorphism
  6. Message-ID: <334@sps.com>
  7. Date: 13 Aug 92 15:10:10 GMT
  8. Organization: Software Productivity Solutions, Inc (SPS), Melbourne, FL, USA
  9. Lines: 119
  10.  
  11.  
  12. I have a question concerning the code within Alex's article.
  13.  
  14. Alex defined a class hierarchy:
  15.   Shape
  16.     Circle
  17.  
  18. Here are the highlites of his code:
  19.  
  20. >package Shapes is
  21. >
  22. >  type shape is tagged ...;
  23. >
  24. >  procedure draw (x : shape) is <>; -- abstract
  25. >
  26. >end Shapes;
  27. >
  28. >----------------
  29. >with Shapes;
  30. >package Circles is
  31. >
  32. >  type circle is new Shapes.shape ...;  
  33. >   -- inherits primative ops of shape
  34. >
  35. >  procedure draw (x : circle); 
  36. >   -- overide draw, now circle is not abstract
  37. >
  38. >end Circle;
  39. >
  40. >----------------
  41. >with Shapes;
  42. >procedure client (x : shapes.shape) is
  43. >begin
  44. >  -- do something & then call the correct draw procedure
  45. >  -- for the actual parameter x.
  46. >  shapes.draw (x); -- dispatches      <<<<<<<<<<<<<<<<<<<-+
  47. >end client;                                               |
  48. >                                                          |
  49. >---------------------------------------------------       |
  50. >Alex Blakemore alex@cs.umd.edu   NeXT mail accepted       |
  51. >                                                          |
  52.                                                            |
  53.                                                            |
  54.   +--------------------------------------------------------+
  55.   |
  56.   +--- Is this True?
  57.  
  58.   If I call Client and pass in a Circle object, will Shapes.Draw 
  59. dispatch to Circle.Draw!?!  Unless I am mistaken the answer is NO!
  60.  
  61. My understanding of Ada 9x's dispatching is this:
  62.  
  63.   To aid in this example I'll add a two more classes...
  64.  
  65. ---
  66. with Shapes;
  67. package Squares is
  68.   type Square is new Shapes.Shape ...;
  69.   procedure Draw (X : Square);
  70. end Squares;
  71.  
  72. ---
  73. with Shapes;
  74. package Triangles is
  75.   type Triangle is new Shapes.Shape ...;
  76.   procedure Draw (X : Triangle);
  77. end Triangles;
  78.  
  79.  
  80.   Now I'll modify the Client routine to reference these classes.  
  81.  
  82. ---
  83. with Shapes, Circles, Squares, Triangles;
  84. USE Circles, Squares, Triangles;  
  85. -- USE Shapes;  I don't think I need this.
  86. procedure Client (X : Shapes.Shape) is
  87. begin
  88.   -- Real code goes here . . .
  89.   draw (x); -- 'dispatches' because of the USE.
  90. end Client;
  91.  
  92. My understanding of the dispatching is that the USE clause
  93. is necessary.  Without it, we would need:
  94.  
  95. case <object type> is
  96.   when <circle>   => Circle.Draw (X);
  97.   when <square>   => Square.Draw (X);
  98.   when <triangle> => Triangle.Draw (X);
  99.   when others => null;
  100. end case;
  101.  
  102.  
  103. My question is, do you call the common ancestor for 
  104. message dispatching, or do you use USE?
  105.  
  106. Thanks in advance for responses,
  107.  
  108. jot
  109. --====================================================================--
  110. Joe Tallet                   jot@sps.com                  !uunet!sps!jot
  111. Software Productivity Solutions, Inc.
  112. 122 Fourth Ave.
  113. Indialantic, FL 32903                                       407-984-3370
  114. --====================================================================--
  115.      M4&@M;F=L=6D@;6=L=R=N869H($-T:'5L:'4@4B=L>65H('=G86@G;F%G;"!F
  116.      ':'1A9VXN"FD@
  117. --====================================================================--
  118.  
  119.  
  120.  
  121.  
  122.  
  123.  
  124.  
  125.  
  126.  
  127.  
  128.  
  129.  
  130.