home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1993 #3 / NN_1993_3.iso / spool / comp / lang / ada / 4106 < prev    next >
Encoding:
Internet Message Format  |  1993-01-28  |  5.0 KB

  1. Path: sparky!uunet!ulowell!m2c!nic.umass.edu!noc.near.net!inmet!spock!stt
  2. From: stt@spock.camb.inmet.com (Tucker Taft)
  3. Newsgroups: comp.lang.ada
  4. Subject: Re: private types and recompilation
  5. Message-ID: <1993Jan28.152546.25758@inmet.camb.inmet.com>
  6. Date: 28 Jan 93 15:25:46 GMT
  7. References: <7277@grus.cs.nps.navy.mil> <9301271722.aa25516@Paris.ics.uci.edu>
  8. Sender: news@inmet.camb.inmet.com
  9. Organization: Intermetrics Inc, Cambridge MA
  10. Lines: 110
  11. Nntp-Posting-Host: spock
  12.  
  13. In article <9301271722.aa25516@Paris.ics.uci.edu> 
  14.   kanderso@mabillon.ICS.UCI.EDU (Kenneth Anderson) writes:
  15.  
  16. >In comp.lang.ada you write:
  17. >
  18. >>When Ada 83 was designed, why did the designers choose to put
  19. >>the details of private types in package specifications, rather than
  20. >>in package bodies (which is more in the spirit of information hiding, and
  21. >>better supports independent compilation).
  22. >
  23. >I'm not sure, but I think because the compiler needs to know the size
  24. >of the types so that it can allocate space for parameters in the
  25. >subprograms that are defined in the spec.
  26.  
  27. Parameters are not a great problem, because they can in general
  28. be passed by reference (though if the full type is a scalar or
  29. access type, pass by copy is required).  However, to be able
  30. to allocate local variables of a private type outside the
  31. defining package, and to implement the "=" operator and ":=" operation
  32. correctly, some knowledge of the representation is required.
  33. It is possible to defer all of this to link-time, but for languages
  34. like Ada (and C++, by the way), the choice was made in favor
  35. of minimizing run-time overhead, at the expense of more recompilation.
  36.  
  37. A general principle in Ada is that every abstraction has
  38. a "logical interface" (the visible part), a "physical interface" (the
  39. private part), and an "implementation" (the body).
  40. If you don't change the logical interface, then you don't have
  41. to edit the source of the clients.  If you don't change the
  42. physical interface, then you don't have to recompile the clients;
  43. only relink.
  44.  
  45. This principle of separation is carried out quite consistently 
  46. throughout the language, and has been maintained (and somewhat
  47. enhanced with private child packages) in Ada 9X.
  48.  
  49. As far as the official rationale, here is a paragraph from
  50. the '86 Rationale for the Design of the Ada Programming Language
  51. (section 9.3.3 "Influence of Separate Compilation on the
  52. Design of Packages," page 183):
  53.  
  54.     The declaration of a private type therefore does not in itself
  55.     provide enough information for storage allocation and other
  56.     operations.  The full declaration of the type is needed, and so
  57.     is any representation clause that the user wants: storage allocation
  58.     will therefore require the information provided by the private part.
  59.     Note that placing this information in the package body would not
  60.     be satisfactory since it would create unnecessary dependences of
  61.     other compilation units on this body, with the consequence that
  62.     changes in the algorithms provided in the body would require
  63.     recompilation of these other compilation units, even in the
  64.     absence of change to the full type declaration.
  65.  
  66. As a relatively late change to Ada 83, the rules for "incomplete"
  67. types were relaxed somewhat, so that if a private type was
  68. in fact an access type, then the full definition of its designated
  69. type could be deferred into the package body, as mentioned
  70. in the response by Ken Anderson:
  71.   
  72. >>generic
  73. >>  type ATOM is private;
  74. >>package LIST_ADT is
  75. >>  type POSITION is private;
  76. >>  type LIST is private;
  77. >>  procedure CREATE(L: in out LIST);
  78. >>  procedure INSERT_AFTER(L: in out LIST; P: POSITION; A: ATOM);
  79. >>  ...
  80. >
  81. >>private
  82. >>  type LIST;
  83. >>  type POSITION is access LIST;
  84. >
  85. >You only need the previous two lines.  The declaration below can be
  86. >hidden in the body of the package.
  87. >
  88. >>  type LIST is record
  89. >>    A: ATOM;
  90. >>    NEXT: POSITION;
  91. >>  end record;
  92. >>end LIST_ADT;
  93. >
  94. >
  95. >>-Dave Erickson
  96. >
  97. >Ken
  98.  
  99. By the way, in Ada 9X, there is another alternative, namely an
  100. "abstract tagged type."  Such a type (say, "T") can be used as the root 
  101. of a hierarchy of (tagged) types, and the corresponding "class-wide 
  102. type" (T'CLASS) can be used as the type of a subprogram formal parameter, 
  103. allowing algorithms to be written that have no knowledge of the
  104. reprsentation of the various derivatives of the abstract type.
  105.  
  106. Such "class-wide" algorithms work by relying strictly 
  107. on the "primitive" operations of the abstract type.
  108. Parameters of a class-wide type are naturally passed by reference,
  109. and calls on the primitive operations of such a type are dynamically 
  110. bound to the appropriate operation, based on a "tag" that identifies
  111. the specific type of the actual object associated with the given formal
  112. parameter.  It is interesting to note that the '86 Rationale foreshadowed
  113. this capability (quoting from earlier in section 9.3.3):
  114.  
  115.     ...  A more flexible architecture -- evolved perhaps from today's
  116.     "tagged architectures" -- would indeed allow data representation
  117.     choices to be deferred until link time, or even later.
  118.  
  119. S. Tucker Taft    stt@inmet.com
  120. Ada 9X Mapping/Revision Team
  121. Intermetrics, Inc.
  122. Cambridge, MA  02138
  123.