Inference of Dependent Types in Generic Instantiations (GNAT Reference Manual) (original) (raw)
17.3.9 Inference of Dependent Types in Generic Instantiations ¶
If a generic formal type T2 depends on another formal type T1, the actual for T1 can be inferred from the actual for T2. That is, you can give the actual for T2, and leave out the one for T1.
For example, Ada.Unchecked_Deallocation
has two generic formals:
generic type Object (<>) is limited private; type Name is access Object; procedure Ada.Unchecked_Deallocation (X : in out Name);
where Name
depends on Object
. With this language extension, you can leave out the actual for Object
, as in:
type Integer_Access is access all Integer;
procedure Free is new Unchecked_Deallocation (Name => Integer_Access);
The compiler will infer that the actual type for Object
is Integer
. Note that named notation is always required when using inference.
The following inferences are allowed:
- For a formal access type, the designated type can be inferred.
- For a formal array type, the index type(s) and the component type can be inferred.
- For a formal type with discriminants, the type(s) of the discriminants can be inferred.
Example for arrays:
generic type Element_Type is private; type Index_Type is (<>); type Array_Type is array (Index_Type range <>) of Element_Type; package Array_Operations is ... end Array_Operations;
...
type Int_Array is array (Positive range <>) of Integer;
package Int_Array_Operations is new Array_Operations (Array_Type => Int_Array);
The index and component types of Array_Type
are inferred fromInt_Array
, so that the above instantiation is equivalent to the following standard-Ada instantiation:
package Int_Array_Operations is new Array_Operations (Element_Type => Integer, Index_Type => Positive, Array_Type => Int_Array);