1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274
|
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<HTML>
<HEAD>
<TITLE>AARM95 - Type Extensions</TITLE>
<META NAME="Author" CONTENT="JTC1/SC22/WG9/ARG, by Randall Brukardt, ARG Editor">
<META NAME="GENERATOR" CONTENT="Arm_Form.Exe, Ada Reference Manual generator">
<STYLE type="text/css">
DIV.paranum {position: absolute; font-family: Arial, Helvetica, sans-serif; left: 0.5 em; top: auto}
TT {font-family: "Courier New", monospace}
DT {display: compact}
DIV.Normal {font-family: "Times New Roman", Times, serif; margin-bottom: 0.6em}
DIV.Wide {font-family: "Times New Roman", Times, serif; margin-top: 0.6em; margin-bottom: 0.6em}
DIV.Annotations {font-family: "Times New Roman", Times, serif; margin-left: 4.0em; margin-bottom: 0.6em}
DIV.WideAnnotations {font-family: "Times New Roman", Times, serif; margin-left: 4.0em; margin-top: 0.6em; margin-bottom: 0.6em}
DIV.Index {font-family: "Times New Roman", Times, serif}
DIV.SyntaxSummary {font-family: "Times New Roman", Times, serif; margin-left: 2.0em; margin-bottom: 0.4em}
DIV.Notes {font-family: "Times New Roman", Times, serif; margin-left: 2.0em; margin-bottom: 0.6em}
DIV.NotesHeader {font-family: "Times New Roman", Times, serif; margin-left: 2.0em}
DIV.SyntaxIndented {font-family: "Times New Roman", Times, serif; margin-left: 2.0em; margin-bottom: 0.4em}
DIV.Indented {font-family: "Times New Roman", Times, serif; margin-left: 6.0em; margin-bottom: 0.6em}
DIV.CodeIndented {font-family: "Times New Roman", Times, serif; margin-left: 4.0em; margin-bottom: 0.6em}
DIV.SmallIndented {font-family: "Times New Roman", Times, serif; margin-left: 10.0em; margin-bottom: 0.6em}
DIV.SmallCodeIndented {font-family: "Times New Roman", Times, serif; margin-left: 8.0em; margin-bottom: 0.6em}
DIV.Examples {font-family: "Courier New", monospace; margin-left: 2.0em; margin-bottom: 0.6em}
DIV.SmallExamples {font-family: "Courier New", monospace; font-size: 80%; margin-left: 7.5em; margin-bottom: 0.6em}
DIV.IndentedExamples {font-family: "Courier New", monospace; margin-left: 8.0em; margin-bottom: 0.6em}
DIV.SmallIndentedExamples {font-family: "Courier New", monospace; font-size: 80%; margin-left: 15.0em; margin-bottom: 0.6em}
UL.Bulleted {font-family: "Times New Roman", Times, serif; margin-left: 2.0em; margin-right: 2.0em; margin-top: 0em; margin-bottom: 0.5em}
UL.SmallBulleted {font-family: "Times New Roman", Times, serif; margin-left: 6.0em; margin-right: 6.0em; margin-top: 0em; margin-bottom: 0.5em}
UL.NestedBulleted {font-family: "Times New Roman", Times, serif; margin-left: 4.0em; margin-right: 4.0em; margin-top: 0em; margin-bottom: 0.5em}
UL.SmallNestedBulleted {font-family: "Times New Roman", Times, serif; margin-left: 8.0em; margin-right: 8.0em; margin-top: 0em; margin-bottom: 0.5em}
UL.IndentedBulleted {font-family: "Times New Roman", Times, serif; margin-left: 8.0em; margin-right: 8.0em; margin-top: 0em; margin-bottom: 0.5em}
UL.CodeIndentedBulleted {font-family: "Times New Roman", Times, serif; margin-left: 6.0em; margin-right: 6.0em; margin-top: 0em; margin-bottom: 0.5em}
UL.CodeIndentedNestedBulleted {font-family: "Times New Roman", Times, serif; margin-left: 8.0em; margin-right: 8.0em; margin-top: 0em; margin-bottom: 0.5em}
UL.SyntaxIndentedBulleted {font-family: "Times New Roman", Times, serif; margin-left: 4.0em; margin-right: 4.0em; margin-top: 0em; margin-bottom: 0.5em}
UL.NotesBulleted {font-family: "Times New Roman", Times, serif; margin-left: 4.0em; margin-right: 4.0em; margin-top: 0em; margin-bottom: 0.5em}
UL.NotesNestedBulleted {font-family: "Times New Roman", Times, serif; margin-left: 6.0em; margin-right: 6.0em; margin-top: 0em; margin-bottom: 0.5em}
DL.Hanging {font-family: "Times New Roman", Times, serif; margin-top: 0em; margin-bottom: 0.6em}
DD.Hanging {margin-left: 6.0em}
DL.IndentedHanging {font-family: "Times New Roman", Times, serif; margin-left: 4.0em; margin-top: 0em; margin-bottom: 0.6em}
DD.IndentedHanging {margin-left: 2.0em}
DL.HangingInBulleted {font-family: "Times New Roman", Times, serif; margin-left: 2.0em; margin-right: 2.0em; margin-top: 0em; margin-bottom: 0.5em}
DD.HangingInBulleted {margin-left: 4.0em}
DL.SmallHanging {font-family: "Times New Roman", Times, serif; margin-left: 4.0em; margin-top: 0em; margin-bottom: 0.6em}
DD.SmallHanging {margin-left: 7.5em}
DL.SmallIndentedHanging {font-family: "Times New Roman", Times, serif; margin-left: 8.0em; margin-top: 0em; margin-bottom: 0.6em}
DD.SmallIndentedHanging {margin-left: 2.0em}
DL.SmallHangingInBulleted {font-family: "Times New Roman", Times, serif; margin-left: 6.0em; margin-right: 6.0em; margin-top: 0em; margin-bottom: 0.5em}
DD.SmallHangingInBulleted {margin-left: 5.0em}
DL.Enumerated {font-family: "Times New Roman", Times, serif; margin-right: 0.0em; margin-top: 0em; margin-bottom: 0.5em}
DD.Enumerated {margin-left: 2.0em}
DL.SmallEnumerated {font-family: "Times New Roman", Times, serif; margin-left: 4.0em; margin-right: 4.0em; margin-top: 0em; margin-bottom: 0.5em}
DD.SmallEnumerated {margin-left: 2.5em}
DL.NestedEnumerated {font-family: "Times New Roman", Times, serif; margin-left: 2.0em; margin-right: 2.0em; margin-top: 0em; margin-bottom: 0.5em}
DL.SmallNestedEnumerated {font-family: "Times New Roman", Times, serif; margin-left: 6.0em; margin-right: 6.0em; margin-top: 0em; margin-bottom: 0.5em}
</STYLE>
</HEAD>
<BODY TEXT="#000000" BGCOLOR="#FFFFF0" LINK="#0000FF" VLINK="#800080" ALINK="#FF0000">
<P><A HREF="AA-TOC.html">Contents</A> <A HREF="AA-0-29.html">Index</A> <A HREF="AA-3-9.html">Previous</A> <A HREF="AA-3-9-2.html">Next</A></P>
<HR>
<H1> 3.9.1 Type Extensions</H1>
<DIV Class="Paranum"><FONT SIZE=-2>1</FONT></DIV>
<DIV Class="Normal"> [<A NAME="I2076"></A> <A NAME="I2077"></A><A NAME="I2078"></A><A NAME="I2079"></A><A NAME="I2080"></A><A NAME="I2081"></A>Every
type extension is a tagged type, and is either a <I>record extension</I>
or a <I>private extension</I> of some other tagged type.] </DIV>
<H4 ALIGN=CENTER>Language Design Principles</H4>
<DIV Class="Paranum"><FONT SIZE=-2>1.a</FONT></DIV>
<DIV Class="Annotations"><FONT SIZE=-1>We want to make sure that we can
extend a generic formal tagged type, without knowing its discriminants.</FONT></DIV>
<DIV Class="Paranum"><FONT SIZE=-2>1.b</FONT></DIV>
<DIV Class="Annotations"><FONT SIZE=-1>We don't want to allow components
in an extension aggregate to depend on discriminants inherited from the
parent value, since such dependence requires staticness in aggregates,
at least for variants. </FONT></DIV>
<H4 ALIGN=CENTER>Syntax</H4>
<DIV Class="Paranum"><FONT SIZE=-2>2</FONT></DIV>
<DIV Class="SyntaxIndented"><FONT FACE="Arial, Helvetica">record_extension_part<A NAME="I2082"></A>
::= </FONT><B>with</B> <A NAME="I2083"></A><FONT FACE="Arial, Helvetica">record_definition</FONT></DIV>
<H4 ALIGN=CENTER>Legality Rules</H4>
<DIV Class="Paranum"><FONT SIZE=-2>3</FONT></DIV>
<DIV Class="Normal"> The parent type of a record extension shall not
be a class-wide type. If the parent type is nonlimited, then each of
the components of the <FONT FACE="Arial, Helvetica">record_extension_part</FONT>
shall be nonlimited. <A NAME="I2084"></A>The accessibility level (see
<A HREF="AA-3-10-2.html">3.10.2</A>) of a record extension shall not
be statically deeper than that of its parent type. <A NAME="I2085"></A>In
addition to the places where Legality Rules normally apply (see <A HREF="AA-12-3.html">12.3</A>),
these rules apply also in the private part of an instance of a generic
unit. </DIV>
<DIV Class="Paranum"><FONT SIZE=-2>3.a</FONT></DIV>
<DIV Class="Annotations"><FONT SIZE=-1><B>Reason: </B>If the parent is
a limited formal type, then the actual might be nonlimited.</FONT></DIV>
<DIV Class="Paranum"><FONT SIZE=-2>3.b</FONT></DIV>
<DIV Class="Annotations"><FONT SIZE=-1>A similar accessibility rule is
not needed for private extensions, because in a package, the rule will
apply to the <FONT FACE="Arial, Helvetica">full_type_declaration</FONT>,
and for a generic formal private extension, the actual is all that matters.</FONT></DIV>
<DIV Class="Paranum"><FONT SIZE=-2>4</FONT></DIV>
<DIV Class="Normal"> A type extension shall not be declared in a generic
body if the parent type is declared outside that body. </DIV>
<DIV Class="Paranum"><FONT SIZE=-2>4.a</FONT></DIV>
<DIV Class="Annotations"><FONT SIZE=-1><B>Reason: </B>This paragraph
ensures that a dispatching call will never attempt to execute an inaccessible
subprogram body.</FONT></DIV>
<DIV Class="Paranum"><FONT SIZE=-2>4.b</FONT></DIV>
<DIV Class="Annotations"><FONT SIZE=-1>The part about generic bodies
is necessary in order to preserve the contract model.</FONT></DIV>
<DIV Class="Paranum"><FONT SIZE=-2>4.c</FONT></DIV>
<DIV Class="Annotations" Style="margin-bottom: 0.4em"><FONT SIZE=-1>Since
a generic unit can be instantiated at a deeper accessibility level than
the generic unit, it is necessary to prevent type extensions whose parent
is declared outside the generic unit. The same is true if the parent
is a formal of the generic unit. If the parent is declared in the <FONT FACE="Arial, Helvetica">generic_declaration</FONT>
(but is not a formal), we don't run afoul of the accessibility rules,
because we know that the instance declaration and body will be at the
same accessibility level. However, we still have a problem in that case,
because it might have an unknown number of abstract subprograms, as in
the following example: </FONT></DIV>
<DIV Class="Paranum"><FONT SIZE=-2>4.d</FONT></DIV>
<DIV Class="SmallExamples"><TT><B>package</B> P <B>is</B><BR>
<B>type</B> T <B>is</B> <B>tagged</B> <B>null</B> <B>record</B>;<BR>
<B>function</B> F <B>return</B> T; --<I> Inherited versions will be abstract.</I><BR>
<B>end</B> P;</TT></DIV>
<DIV Class="Paranum"><FONT SIZE=-2>4.e</FONT></DIV>
<DIV Class="SmallExamples"><TT><B>generic</B><BR>
<B>type</B> TT <B>is</B> <B>tagged</B> <B>private</B>;<BR>
<B>package</B> Gp <B>is</B><BR>
<B>type</B> NT <B>is</B> <B>abstract new</B> TT <B>with</B> <B>null</B> <B>record</B>;<BR>
<B>procedure</B> Q(X : <B>in</B> NT) <B>is abstract</B>;<BR>
<B>end</B> Gp;</TT></DIV>
<DIV Class="Paranum"><FONT SIZE=-2>4.f</FONT></DIV>
<DIV Class="SmallExamples"><TT><B>package</B> <B>body</B> Gp <B>is</B><BR>
<B>type</B> NT2 <B>is</B> <B>new</B> NT <B>with</B> <B>null</B> <B>record</B>; --<I> Illegal!</I><BR>
<B>procedure</B> Q(X : <B>in</B> NT2) <B>is</B> <B>begin</B> <B>null</B>; <B>end</B> Q;<BR>
--<I> Is this legal or not? Can't decide because</I><BR>
--<I> we don't know whether TT had any functions that go abstract</I><BR>
--<I> on extension.</I><BR>
<B>end</B> Gp;</TT></DIV>
<DIV Class="Paranum"><FONT SIZE=-2>4.g</FONT></DIV>
<DIV Class="SmallExamples"><TT><B>package</B> I <B>is</B> <B>new</B> Gp(TT => P.T);</TT></DIV>
<DIV Class="Paranum"><FONT SIZE=-2>4.h</FONT></DIV>
<DIV Class="Annotations"><FONT SIZE=-1>I.NT is an abstract type with
two abstract subprograms: F (inherited as abstract) and Q (explicitly
declared as abstract). But the generic body doesn't know about F, so
we don't know that it needs to be overridden to make a nonabstract extension
of NT. Furthermore, a formal tagged limited private type can be extended
with limited components, but the actual might not be limited, which would
allow assignment of limited types, which is bad. Hence, we have to disallow
this case as well.</FONT></DIV>
<DIV Class="Paranum"><FONT SIZE=-2>4.i</FONT></DIV>
<DIV Class="Annotations"><FONT SIZE=-1>If TT were declared as abstract,
then we could have the same problem with abstract procedures.</FONT></DIV>
<DIV Class="Paranum"><FONT SIZE=-2>4.j</FONT></DIV>
<DIV Class="Annotations"><FONT SIZE=-1>We considered disallowing all
tagged types in a generic body, for simplicity. We decided not to go
that far, in order to avoid unnecessary restrictions.</FONT></DIV>
<DIV Class="Paranum"><FONT SIZE=-2>4.k</FONT></DIV>
<DIV Class="Annotations"><FONT SIZE=-1><A NAME="I2086"></A>We also considered
trying make the accessibility level part of the contract; i.e. invent
some way of saying (in the <FONT FACE="Arial, Helvetica">generic_declaration</FONT>)
``all instances of this generic unit will have the same accessibility
level as the <FONT FACE="Arial, Helvetica">generic_declaration</FONT>.''
Unfortunately, that doesn't solve the part of the problem having to do
with abstract types.</FONT></DIV>
<DIV Class="Paranum"><FONT SIZE=-2>4.l</FONT></DIV>
<DIV Class="Annotations"><FONT SIZE=-1>Children of generic units obviate
the need for extension in the body somewhat. </FONT></DIV>
<H4 ALIGN=CENTER>Dynamic Semantics</H4>
<DIV Class="Paranum"><FONT SIZE=-2>5</FONT></DIV>
<DIV Class="Normal"> <A NAME="I2087"></A>The elaboration of a <FONT FACE="Arial, Helvetica">record_extension_part</FONT>
consists of the elaboration of the <FONT FACE="Arial, Helvetica">record_definition</FONT>.
</DIV>
<DIV Class="NotesHeader"><FONT SIZE=-1>NOTES</FONT></DIV>
<DIV Class="Paranum"><FONT SIZE=-2>6</FONT></DIV>
<DIV Class="Notes"><FONT SIZE=-1>66 The term ``type extension''
refers to a type as a whole. The term ``extension part'' refers to the
piece of text that defines the additional components (if any) the type
extension has relative to its specified ancestor type. </FONT></DIV>
<DIV Class="Paranum"><FONT SIZE=-2>6.a</FONT></DIV>
<DIV Class="Annotations"><FONT SIZE=-1><B>Discussion: </B>We considered
other terminology, such as ``extended type.'' However, the terms ``private
extended type'' and ``record extended type'' did not convey the proper
meaning. Hence, we have chosen to uniformly use the term ``extension''
as the type resulting from extending a type, with ``private extension''
being one produced by privately extending the type, and ``record extension''
being one produced by extending the type with an additional record-like
set of components. Note also that the term ``type extension'' refers
to the result of extending a type in the language Oberon as well (though
there the term ``extended type'' is also used, interchangeably, perhaps
because Oberon doesn't have the concept of a ``private extension'').
</FONT></DIV>
<DIV Class="Paranum"><FONT SIZE=-2>7</FONT></DIV>
<DIV Class="Notes"><FONT SIZE=-1>67 The accessibility rules
imply that a tagged type declared in a library <FONT FACE="Arial, Helvetica">package_specification</FONT>
can be extended only at library level or as a generic formal. When the
extension is declared immediately within a <FONT FACE="Arial, Helvetica">package_body</FONT>,
primitive subprograms are inherited and are overridable, but new primitive
subprograms cannot be added.</FONT></DIV>
<DIV Class="Paranum"><FONT SIZE=-2>8</FONT></DIV>
<DIV Class="Notes"><FONT SIZE=-1>68 A <FONT FACE="Arial, Helvetica">name</FONT>
that denotes a component (including a discriminant) of the parent type
is not allowed within the <FONT FACE="Arial, Helvetica">record_extension_part</FONT>.
Similarly, a <FONT FACE="Arial, Helvetica">name</FONT> that denotes a
component defined within the <FONT FACE="Arial, Helvetica">record_extension_part</FONT>
is not allowed within the <FONT FACE="Arial, Helvetica">record_extension_part</FONT>.
It is permissible to use a <FONT FACE="Arial, Helvetica">name</FONT>
that denotes a discriminant of the record extension, providing there
is a new <FONT FACE="Arial, Helvetica">known_discriminant_part</FONT>
in the enclosing type declaration. (The full rule is given in <A HREF="AA-3-8.html">3.8</A>.)
</FONT></DIV>
<DIV Class="Paranum"><FONT SIZE=-2>8.a</FONT></DIV>
<DIV Class="Annotations"><FONT SIZE=-1><B>Reason: </B>The restriction
against depending on discriminants of the parent is to simplify the definition
of extension aggregates. The restriction against using parent components
in other ways is methodological; it presumably simplifies implementation
as well. </FONT></DIV>
<DIV Class="Paranum"><FONT SIZE=-2>9</FONT></DIV>
<DIV Class="Notes"><FONT SIZE=-1>69 Each visible component
of a record extension has to have a unique name, whether the component
is (visibly) inherited from the parent type or declared in the <FONT FACE="Arial, Helvetica">record_extension_part</FONT>
(see <A HREF="AA-8-3.html">8.3</A>). </FONT></DIV>
<H4 ALIGN=CENTER>Examples</H4>
<DIV Class="Paranum"><FONT SIZE=-2>10</FONT></DIV>
<DIV Class="Normal" Style="margin-bottom: 0.4em"> <I>Examples of
record extensions (of types defined above in <A HREF="AA-3-9.html">3.9</A>):</I>
</DIV>
<DIV Class="Paranum"><FONT SIZE=-2>11</FONT></DIV>
<DIV Class="Examples"><TT><B>type</B> Painted_Point <B>is new</B> Point <B>with</B><BR>
<B>record</B><BR>
Paint : Color := White;<BR>
<B>end record</B>;<BR>
<I>-- Components X and Y are inherited</I></TT></DIV>
<DIV Class="Paranum"><FONT SIZE=-2>12</FONT></DIV>
<DIV Class="Examples"><TT>Origin : <B>constant</B> Painted_Point := (X | Y => 0.0, Paint => Black);</TT></DIV>
<DIV Class="Paranum"><FONT SIZE=-2>13</FONT></DIV>
<DIV Class="Examples"><TT><B>type</B> Literal <B>is new</B> Expression <B>with</B><BR>
<B>record</B> <I>-- a leaf in an Expression tree</I><BR>
Value : Real;<BR>
<B>end record</B>;</TT></DIV>
<DIV Class="Paranum"><FONT SIZE=-2>14</FONT></DIV>
<DIV Class="Examples"><TT><B>type</B> Expr_Ptr <B>is access all</B> Expression'Class;<BR>
<I>-- see <A HREF="AA-3-10.html">3.10</A></I></TT></DIV>
<DIV Class="Paranum"><FONT SIZE=-2>15</FONT></DIV>
<DIV Class="Examples"><TT><B>type</B> Binary_Operation <B>is new</B> Expression <B>with</B><BR>
<B>record</B> <I>-- an internal node in an Expression tree</I><BR>
Left, Right : Expr_Ptr;<BR>
<B>end record</B>;</TT></DIV>
<DIV Class="Paranum"><FONT SIZE=-2>16</FONT></DIV>
<DIV Class="Examples"><TT><B>type</B> Addition <B>is new</B> Binary_Operation <B>with null record</B>;<BR>
<B>type</B> Subtraction <B>is new</B> Binary_Operation <B>with null record</B>;<BR>
<I>-- No additional components needed for these extensions</I></TT></DIV>
<DIV Class="Paranum"><FONT SIZE=-2>17</FONT></DIV>
<DIV Class="Examples"><TT>Tree : Expr_Ptr := <I>-- A tree representation of ``5.0 + (13.0-7.0)''</I><BR>
<B>new</B> Addition'(<BR>
Left => <B>new</B> Literal'(Value => 5.0),<BR>
Right => <B>new</B> Subtraction'(<BR>
Left => <B>new</B> Literal'(Value => 13.0),<BR>
Right => <B>new</B> Literal'(Value => 7.0)));</TT></DIV>
<H4 ALIGN=CENTER>Extensions to Ada 83</H4>
<DIV Class="Paranum"><FONT SIZE=-2>17.a</FONT></DIV>
<DIV Class="Annotations"><FONT SIZE=-1><A NAME="I2088"></A>Type extension
is a new concept. </FONT></DIV>
<HR>
<P><A HREF="AA-TOC.html">Contents</A> <A HREF="AA-0-29.html">Index</A> <A HREF="AA-3-9.html">Previous</A> <A HREF="AA-3-9-2.html">Next</A> <A HREF="AA-TTL.html">Legal</A></P>
</BODY>
</HTML>
|