Parameters to Generic Packages and Procedures
Parameters for Generic Packages and Procedures
The Problem
- Generic packages and generic procedures can have types as parameters, and
- Ada has a very rich type model
- Thus, need a notation to specify what kind of type is allowed as a generic parameter
- Question: Can a parameter to a generic have a default value?
Generic Parameters that are Objects
- A Generic parameter that is an object
generic
size : in Natural;
This uses the term object to mean something that has a value
- It does NOT mean object in the way it's used in Object Oriented Programming
- Although OO objects also have values
Generic Parameters that are Types
- Possible generic parameters that are types
generic
type X is limited private -- any type at all
type X is private -- any type that is not limited
type X is (<>) -- any discrete (integer or enumeration) type
type X is range <> -- any signed integer type
type X is mod <> -- any modular integer type
type X is digits <> -- any floating point type
type X is delta <> -- any fixed point type
type X is delta <> digits <> -- any decimal type
type X is access Y -- any access-to-Y type
type X is access all Y -- any "access all Y" type
type X is access constant Y -- any "access constant Y" type
type X is array (Y range <>) of Z -- any UNCONSTRAINED array-of-Z type with a subtype of Y as its index subtype
type X is array (Y) of Z -- any CONSTRAINED array-of-Z type with a subtype of Y as its index subtype
type X is new Y -- any type derived from Y
type X is new Y with private -- any non-abstract tagged type derived from Y
type X is abstract new Y with private -- any tagged type (abstract or not) derived from Y
type X is tagged private -- any non-abstract non-limited tagged type
type X is tagged limited private -- any non-abstract tagged type
type X is abstract tagged private -- any tagged type that is not limited
type X is abstract tagged limited private -- any tagged type at all
Generic Parameters that are Procedures and Functions
- Parameters to generic packages (and procedures and functions) can also be:
- Procedures and functions, themselves
- Example:
generic
type ItemType is private;
with function "="(Left, Right: ItemType) return Boolean;
package SomePkg is
...
end SomePkg;
...
package body SomePkg is
...
procedure x(s1, s2: ItemType) is
begin
if "="(s1, s2) then
...
if s1 = s2 then
end SomePkg;
Client:
type Student is record ...
function eq(x, y: Student) return boolean is
begin
return x.ID = y.ID;
end eq;
package myPkg is new SomePkg(
ItemType => Student,
"=" => eq
);
Generic Parameters that are Packages
- Parameters to generic packages (and procedures and functions) can also be
instantiations of other generic packages
- Example:
generic
type ItemType is private;
package whatever is new SomePkg(<>);
package SomePkg2 is
Default Values for Generic Parameters
- It's simple to declare a default value for a parameter for a generic
- Examples:
generic
Object : in Natural := 0;
type ItemType is private;
with function "="(Left, Right: ItemType) return Boolean is <>
package MyPkg is
The Box (ie <>) specifies that the default = for ItemType is used if this parameter is
not specified