|
| 1 | +- Feature Name: Class-Wide Access Subtype |
| 2 | +- Start Date: 2021-01-05 |
| 3 | +- RFC PR: |
| 4 | +- RFC Issue: |
| 5 | + |
| 6 | +Summary |
| 7 | +======= |
| 8 | + |
| 9 | +We propose to add a new constraint kind to restrict values of class-wide |
| 10 | +access types. Values of constrained subtype could point to a given derived |
| 11 | +type or a hierarchy. As with other subtypes this allows us to have |
| 12 | +a more precise and clean code, eliminates the need of many extra |
| 13 | +type-conventions and extra subtype declarations. Example: |
| 14 | + |
| 15 | +.. code-block:: ada |
| 16 | + type Shape is abstract tagged private; |
| 17 | + type Shape_Access is access all Shape'Class; |
| 18 | + type Cube is new Shape with private; |
| 19 | + My_Cube : Shape_Access for access Cube := new Cube; |
| 20 | +
|
| 21 | +Now My_Cube.all designates a Cube object, but My_Cube still has |
| 22 | +Shape_Access type. So you can initialize/use the object without |
| 23 | +an extra convention to the Cube and in the same time you can use |
| 24 | +the pointer where class-wide Shape_Access expected. |
| 25 | + |
| 26 | +Motivation |
| 27 | +========== |
| 28 | + |
| 29 | +Subtypes are an important part of the Ada language. It makes code |
| 30 | +more expressive and precise allowing both the reader and the compiler |
| 31 | +better understand the author intend. |
| 32 | + |
| 33 | +But for now access types has only null-exclusion constraint. |
| 34 | + |
| 35 | +Proposed new constraint allows a restriction based on referenced values: |
| 36 | +a restricted subtype can point only to the given derived type or class-wide |
| 37 | +type. Having this restricted value the author doesn't need to convert |
| 38 | +dereferenced value to the derived type. |
| 39 | + |
| 40 | +As an example let's consider a typical pattern in OOP style. We declare a |
| 41 | +type hierarchy for geomerty shapes and a procedure to register shape objects. |
| 42 | + |
| 43 | +.. code-block:: ada |
| 44 | + type Shape is abstract tagged null record; |
| 45 | + type Shape_Access is access all Shape'Class; |
| 46 | + procedure Register (Object : Shape_Access); |
| 47 | + type Rectangle is new Shape with record |
| 48 | + Width, Height : Natural; |
| 49 | + end record; |
| 50 | +
|
| 51 | +Next code registers a Rectangle and a circle without using a new constraints. |
| 52 | +The first approach uses an extra access type: |
| 53 | + |
| 54 | +.. code-block:: ada |
| 55 | + type Rectangle_Access is access all Rectangle; -- an extra type |
| 56 | + declare |
| 57 | + My_Rectangle : Rectangle_Access := new Rectangle; |
| 58 | + begin |
| 59 | + My_Rectangle.Width := 10; |
| 60 | + My_Rectangle.Heigth := 5; |
| 61 | + Register (Shape_Access (My_Rectangle)); -- an extra type convention |
| 62 | + end; |
| 63 | +
|
| 64 | +The first approach uses an extra type convention: |
| 65 | + |
| 66 | +.. code-block:: ada |
| 67 | + declare |
| 68 | + My_Rectangle : Shape_Access := new Rectangle; |
| 69 | + begin |
| 70 | + Rectangle (My_Rectangle).Width := 10; -- an extra type convention |
| 71 | + Rectangle (My_Rectangle).Heigth := 5; -- an extra type convention |
| 72 | + Register (My_Rectangle); |
| 73 | + end; |
| 74 | +
|
| 75 | +With new constraint the code is cleaner: |
| 76 | + |
| 77 | +.. code-block:: ada |
| 78 | + declare |
| 79 | + My_Rectangle : Shape_Access for access Rectangle := new Rectangle; |
| 80 | + begin |
| 81 | + My_Rectangle.Width := 10; -- Dereference denotes Rectangle |
| 82 | + My_Rectangle.Heigth := 5; |
| 83 | + Register (My_Rectangle); -- no extra type convention |
| 84 | + end; |
| 85 | +
|
| 86 | +In many cases new construct replaces anonymous access types. This |
| 87 | +eliminates several issues with anonymous access types: |
| 88 | + |
| 89 | +- accessibility level of object is "not clear" in many cases, in |
| 90 | + particular when object allocated in the call of a subprogram |
| 91 | +- when passed object need to be stored somewhere it can't be safely |
| 92 | + converted to named access type |
| 93 | +- use of .all'Unchecked_Access/.all'Unrestricted_Access doesn't work |
| 94 | + for 'null' pointer |
| 95 | + |
| 96 | +All of these issues could be detected only during execution, and sometimes |
| 97 | +in corner cases only. |
| 98 | + |
| 99 | +---- |
| 100 | +Why are we doing this? What use cases does it support? What is the expected |
| 101 | +outcome? |
| 102 | + |
| 103 | +Guide-level explanation |
| 104 | +======================= |
| 105 | + |
| 106 | +This RFC introduces a new kind of subtype constraint (class_wide_access_constraint). |
| 107 | +It has a syntax form of **for access** *Name*, where *Name* is T or T'Class for some |
| 108 | +tagged type T. The constraint is compatible only with an access-to-object type whose |
| 109 | +designated subtype is a class-wide type. |
| 110 | + |
| 111 | +With this constraint the author could define subtypes: |
| 112 | + |
| 113 | +.. code-block:: ada |
| 114 | + subtype Rectangle_Access is Shape_Access for access Rectangle; |
| 115 | +
|
| 116 | +The Rectangle_Access still has Shape_Access type and can be used whereevere |
| 117 | +Shape_Access is expected. In the same time (implicit or explicit) dereferenced value |
| 118 | +denotes Rectangle type (if the acess value is not null). |
| 119 | + |
| 120 | +This constraint could be used in other places where constraint is allowed. |
| 121 | +For example, |
| 122 | + |
| 123 | +- in an object declaration: |
| 124 | + |
| 125 | +.. code-block:: ada |
| 126 | + My_Rectangle : constant Shape_Access for access Rectangle := new Rectangle; |
| 127 | +
|
| 128 | +- in a return object declartion: |
| 129 | + |
| 130 | +.. code-block:: ada |
| 131 | + return Result : Shape_Access for access Rectangle := new Rectangle do |
| 132 | + Result.Witch := 10; |
| 133 | + Result.Height := 5; |
| 134 | + end return; |
| 135 | +
|
| 136 | +---- |
| 137 | +Explain the proposal as if it was already included in the language and you were |
| 138 | +teaching it to another Ada/SPARK programmer. That generally means: |
| 139 | + |
| 140 | +- Introducing new named concepts. |
| 141 | + |
| 142 | +- Explaining the feature largely in terms of examples. |
| 143 | + |
| 144 | +- Explaining how Ada/SPARK programmers should *think* about the feature, and |
| 145 | + how it should impact the way they use it. It should explain the impact as |
| 146 | + concretely as possible. |
| 147 | + |
| 148 | +- If applicable, provide sample error messages, deprecation warnings, or |
| 149 | + migration guidance. |
| 150 | + |
| 151 | +For implementation-oriented RFCs (e.g. for RFCS that have no or little |
| 152 | +user-facing impact), this section should focus on how compiler contributors |
| 153 | +should think about the change, and give examples of its concrete impact. |
| 154 | + |
| 155 | +For "bug-fixes" RFCs, this section should explain briefly the bug and why it |
| 156 | +matters. |
| 157 | + |
| 158 | +Reference-level explanation |
| 159 | +=========================== |
| 160 | + |
| 161 | +Add to *scalar_constraint* (in 3.2.2) a new rule |
| 162 | + |
| 163 | +.. code-block:: |
| 164 | + scalar_constraint ::= |
| 165 | + range_constraint | digits_constraint | delta_constraint |
| 166 | + | class_wide_access_constraint |
| 167 | + |
| 168 | + class_wide_access_constraint ::= |
| 169 | + **for access** *type_*name |
| 170 | +
|
| 171 | +---- |
| 172 | +This is the technical portion of the RFC. Explain the design in sufficient |
| 173 | +detail that: |
| 174 | + |
| 175 | +- Its interaction with other features is clear. |
| 176 | +- It is reasonably clear how the feature would be implemented. |
| 177 | +- Corner cases are dissected by example. |
| 178 | + |
| 179 | +The section should return to the examples given in the previous section, and |
| 180 | +explain more fully how the detailed proposal makes those examples work. |
| 181 | + |
| 182 | +Rationale and alternatives |
| 183 | +========================== |
| 184 | + |
| 185 | +The nearest feature is anonymous access types, but they have issues (see above). |
| 186 | + |
| 187 | +In our point of view this new constraint kind fits well with Ada philosophy |
| 188 | +and best practices. |
| 189 | + |
| 190 | +---- |
| 191 | +- Why is this design the best in the space of possible designs? |
| 192 | +- What other designs have been considered and what is the rationale for not |
| 193 | + choosing them? |
| 194 | +- What is the impact of not doing this? |
| 195 | +- How does this feature meshes with the general philosophy of the languages ? |
| 196 | + |
| 197 | +Drawbacks |
| 198 | +========= |
| 199 | + |
| 200 | +None :) |
| 201 | + |
| 202 | +---- |
| 203 | +Why should we *not* do this? |
| 204 | + |
| 205 | +Prior art |
| 206 | +========= |
| 207 | + |
| 208 | +This is too Ada specific to have a precedent in other languages, I guess. |
| 209 | + |
| 210 | +---- |
| 211 | +Discuss prior art, both the good and the bad, in relation to this proposal. |
| 212 | + |
| 213 | +- For language, library, and compiler proposals: Does this feature exist in |
| 214 | + other programming languages and what experience have their community had? |
| 215 | + |
| 216 | +- Papers: Are there any published papers or great posts that discuss this? If |
| 217 | + you have some relevant papers to refer to, this can serve as a more detailed |
| 218 | + theoretical background. |
| 219 | + |
| 220 | +This section is intended to encourage you as an author to think about the |
| 221 | +lessons from other languages, provide readers of your RFC with a fuller |
| 222 | +picture. |
| 223 | + |
| 224 | +If there is no prior art, that is fine - your ideas are interesting to us |
| 225 | +whether they are brand new or if it is an adaptation from other languages. |
| 226 | + |
| 227 | +Note that while precedent set by other languages is some motivation, it does |
| 228 | +not on its own motivate an RFC. |
| 229 | + |
| 230 | +Unresolved questions |
| 231 | +==================== |
| 232 | + |
| 233 | +None found yet. |
| 234 | + |
| 235 | +---- |
| 236 | +- What parts of the design do you expect to resolve through the RFC process |
| 237 | + before this gets merged? |
| 238 | + |
| 239 | +- What parts of the design do you expect to resolve through the implementation |
| 240 | + of this feature before stabilization? |
| 241 | + |
| 242 | +- What related issues do you consider out of scope for this RFC that could be |
| 243 | + addressed in the future independently of the solution that comes out of this |
| 244 | + RFC? |
| 245 | + |
| 246 | +Future possibilities |
| 247 | +==================== |
| 248 | + |
| 249 | +No other ideas yet. |
| 250 | + |
| 251 | +---- |
| 252 | +Think about what the natural extension and evolution of your proposal would |
| 253 | +be and how it would affect the language and project as a whole in a holistic |
| 254 | +way. Try to use this section as a tool to more fully consider all possible |
| 255 | +interactions with the project and language in your proposal. |
| 256 | +Also consider how the this all fits into the roadmap for the project |
| 257 | +and of the relevant sub-team. |
| 258 | + |
| 259 | +This is also a good place to "dump ideas", if they are out of scope for the |
| 260 | +RFC you are writing but otherwise related. |
| 261 | + |
| 262 | +If you have tried and cannot think of any future possibilities, |
| 263 | +you may simply state that you cannot think of anything. |
| 264 | + |
| 265 | +Note that having something written down in the future-possibilities section |
| 266 | +is not a reason to accept the current or a future RFC; such notes should be |
| 267 | +in the section on motivation or rationale in this or subsequent RFCs. |
| 268 | +The section merely provides additional information. |
0 commit comments