Generic swap

Revision as of 00:47, 7 September 2007 by rosettacode>Waldorf (New page: Many languages provide a generic programming capability. In C++ this capability is called templates. In Ada it is called generics. A generic routine expresses an algorithm that is indep...)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

Many languages provide a generic programming capability. In C++ this capability is called templates. In Ada it is called generics.

A generic routine expresses an algorithm that is independent of a particular data type. The generic routine is created with one or more generic formal parameters. Those parameters are used during the instantiation process to create an instance of the generic program specialized for the type used as the actual generic parameter.

This task asks you to create a generic swap method that can be used for a wide variety of data types.

Ada

The generic parameters for an Ada generic procedure are defined in a procedure specification, while the algorithm is defined in a procedure body. The first code snippet is the procedure instantiation. The second code snippet is the procedure body.

generic
   type Swap_Type is private;
procedure Generic_Swap(Left : in out Swap_Type; Right : in out Swap_Type);
procedure Generic_Swap(Left : in out Swap_Type; Right : in out Swap_Type) is
   Temp : Swap_Type := Left;
begin
   Left := Right;
   Right := Temp;
end Generic_Swap;