Ada-Rapporteur-Group / User-Community-Input

Ada User Community Input Working Group - Github Mirror Prototype
27 stars 1 forks source link

Anonymous instantiations #50

Open ARG-Editor opened 1 year ago

ARG-Editor commented 1 year ago

This issue continues an unfinished topic from Ada 2022. This issue was created to fulfill the ARG resolution of November 10, 2022.


Declaring a stand-alone object of a generic type can be unnecessarily verbose, requiring the introduction of extra declaration and name (with the extra cognitive overhead of such declarations).

This is particularly noticeable for the containers, where declaring a simple one-time use Vector object is much more verbose than the equivalent array:

Single_Arr : array (Small_Int) of Float;

type Single_Vect_Inst is new Ada.Containers.Vectors (Small_Int, Float); Single_Vect : Single_Vect_Inst.Vector;

There should be a short-hand for simple generic objects.

Similarly, instances of signature packages and interface packages often only need to be created to be passed to a single (outer) instance. Again, we're introducing an extra declaration and name (with the extra cognitive overhead of such declarations).

Two proposals were made for Ada 2022 (but never adopted).

AI12-0215-1 proposed a short-hand which is a direct analog of the anonymous array declaration. It essentially provides syntactic sugar to make the second declaration look much like the first. It also provided a mechanism for naming entities associated with the instance (providing a way to use cursors for containers, which is likely to be needed for many container uses, especially for Lists, Trees, and Sets). It uses prefix notation to access operations of the container. This proposal does not attempt to simplify the use of signature packages and the like.

AI12-0215-2 proposed a more general anonymous instantiation facility that could be used in a (limited) number of contexts. It provided some additional capabilities, but the ability to defined cursors of the anonymous instance is not included. This would limit the utility of the proposal for the declaration of containers where cursor use is almost always required (such as Lists, Trees, and Sets). As in the other proposal, prefix notation would be the only way to call subprograms of an anonymous instance used in an object declaration.

Both proposals elaborated the anonymous instance immediately before the declaration containing the anonymous instance. This avoids needing to restrict the contents of the generic; semantically it is the same as a normal instance, just without the requirement to provide a name.

Neither proposal has complete wording nor has the wording that exists been vetted.

sttaft commented 1 year ago

Raphaël Amiard has done significant work on this topic recently. Hopefully he can provide some of his thinking on these topics in response to this issue.