DEVELOPMENT EDITION · 769442725406
Language Reference
On this page

Generic declarations and instances

A generic describes a package or subprogram parameterized by types, values, operations, or packages. Instantiation supplies actuals and creates an instance with its own declaration identity. This is compile-time language abstraction, not a runtime dictionary of arbitrary Python objects or a promise that structurally identical instances have the same types.

Generic formal categories

Formal Purpose Principal requirements
Private/limited private type Abstract over values with the permitted operations Definite versus unknown-discriminant requirements and copying limits matter
Discrete/signed/modular type Use the corresponding scalar operation family The actual must satisfy that category
Floating/ordinary or decimal fixed type Use supported real/fixed operations The actual must be of the formal's category (digits <>, delta <>, delta <> digits <>)
Array/access/derived/interface type form State a more structured type relationship Actual compatibility follows the supported formal declaration
Object Supply a value such as capacity Its subtype, mode, defaults, and static uses constrain the actual
Subprogram Supply a function/procedure/operator Profile and permitted contract/effect requirements apply
Package Supply an instance of another generic The actual must be the required kind of instance

The syntax reference defines complete formal declarations for Vada and Varc. Vibe's supported PEP 695 type parameters, generic classes, protocol bounds, and inferred calls expose selected categories rather than every Ada generic form independently.

A parameterized package

This Vada fragment declares a stack parameterized by an integer element type and capacity:

generic
   type Element is range <>;
   Capacity : Positive;
package Stacks is
   type Stack is private;
   function Size (S : Stack) return Natural;
   procedure Push (S : in out Stack; X : Element)
     with Pre => Size (S) < Capacity,
          Post => Size (S) = Size (S)'Old + 1;
private
   type Items is array (1 .. Capacity) of Element;
   type Stack is record
      Count : Natural := 0;
      Data : Items := (others => Element'First);
   end record
     with Type_Invariant => Stack.Count <= Capacity;
   function Size (S : Stack) return Natural is (S.Count);
end Stacks;

It requires a conforming body for Push; the maintained generic stack corpus supplies the context. The formal integer type permits integer arithmetic, while Capacity controls the stored array bounds. Push needs a public precondition because callers cannot inspect Count.

Instantiation supplies actuals in formal order or by supported named association. Defaults can supply omitted permitted actuals. A private formal without unknown discriminants requires a definite actual; an unconstrained array without bounds cannot be used where the generic needs to declare an object of that formal directly.

Instances and type identity

Two explicitly declared package instances have distinct declarations and can have distinct types even when their actual parameters are equal. A package renaming provides another name for one instance rather than constructing a second. Do not copy a type annotation from a lookalike instance and assume it denotes the same vector or record type.

The actual for a formal function can also be an attribute that is a function of a subtype (RM 12.6): Integer'Image, T'Succ, Float'Max (Varc Integer::Image). A formal subprogram declared is abstract (optionally with a default, is abstract <>) takes a dispatching operation of a tagged actual, abstract or not, and its calls in the generic dispatch as the actual's do. A formal operator is named by its symbol in a named actual: "=" => "=".

A formal package may name some of its generic's actuals and leave the rest open: with package B is new Boxes (T => Integer, others => <>); (Varc package B = new Boxes<T: Integer, others: <>>). A generic renaming gives a generic a second name, instantiated as the generic is: generic package Containers renames Boxes; (also generic procedure and generic function; in Varc generic package N renames G;, generic fn N renames G; and generic fn N -> _ renames G;).

A generic child of a generic package, generic package Stacks.Peek, is instantiated through an instance of its parent once a with clause names it: package P is new Int_Stacks.Peek;, where Int_Stacks is an instance of Stacks. The child sees the instance's declarations as its parent's, and the parent's formals as the instance's actuals.

Inside a generic unit, its own name denotes the current instance (RM 8.6(18)): in the body of a generic package G, G.Get is the instance's Get, G.Size is the instance's actual for the formal object Size (formal types and subprograms likewise), and a generic function's call of its own name is a recursive call of the instance. A generic subprogram may be declared in any declarative part, a subprogram's included, and completed there by a body, an expression function or a null procedure; Varc may write it with its body at once, generic<type Item = private> fn choose(...) -> Item { ... }.

Varc also permits supported implicit generic instances in qualified type/member paths. The frontend shares an implicit instance for the same resolved generic/actual combination. That sharing policy does not merge independently declared explicit instances. Translation can emit a named instance unit to represent an implicit instance in Vada.

Vibe collection classes and generic classes use implicit typed instances, shared in the same way: list[int] or Box[int] names one instance wherever it is written. See the Vibe reference for generic classes across modules.

Formal subprograms and packages

A formal subprogram gives a statically supplied operation a name inside the generic. An explicit default names an operation; a box default requests the applicable visible operation where the instance is formed. Operators can use the actual type's predefined operation where the accepted form allows it. An actual with a mismatched mode, parameter type, or result does not satisfy the formal merely because it has the same number of arguments.

A formal package describes another generic instance, optionally with unconstrained actuals or specified associations. Its actual must be an instance of the required generic. Names exposed through that formal package refer to the supplied instance's declarations. Do not substitute an unrelated package with similarly named members.

Generic subprogram inference

Supported calls can infer generic actual types from sufficiently informative argument types. A result context alone does not make every arbitrary generic profile inferable. When inference fails, instantiate the generic explicitly or simplify its profile. Do not remove type information merely to silence an error: a literal with universal numeric type can be less informative than a concretely typed object.

Checking and proof

A generic's own text, its specification and body, is checked where it is declared, with each formal standing for every actual it may have: a formal private type is a type whose operations are only those the formal gives, a formal range <> type an integer type of its own, a formal subprogram one of its profile. Its errors are reported at its own lines, once, even when nothing instantiates it; the rules on the formal declarations themselves (a formal object of mode in or in out, a formal type written with a formal type definition, is null only for a procedure) are checked there too. Each instance is checked again with its actuals: that its actuals match its formals (an actual of the formal type's class, of the formal array or access type's definition, a definite subtype for a definite formal), and the rules that depend on the actuals (RM 12.3(11)), reported at the instance.

Verada proves the supported instantiated bodies in the actual type/value context. A generic's source accepting a broad formal category is not evidence that every possible instance is already proved. Each instance must satisfy the resulting constraints, contracts, capacity limits, and termination rules.

Changes to a generic body affect the instances that depend on it; the build/proof artifact system tracks that relationship. The embedded predefined library has a distinct trust policy at user proof sites. A client proving against an instantiated library contract is not the same event as separately proving every library implementation body.

Full generic syntax belongs to the three syntax references.