Tuesday, September 25, 2012

AdaTutor - Advanced Topics (4)

Renaming

A subprogram can be renamed in Ada.  This allows us to avoid the dot notation without a use clause.  For example, if our program withs Ada.Text_IO, we can write:

procedure Print(Object : in String) renames Ada.Text_IO.Put_Line;

We can now call Print instead of Ada.Text_IO.Put_Line.  The old name is still available.  Note that renaming can change the names of the formal parameters ("dummy arguments").  Renaming can also add, delete, or change default values.  When used in a package in Ada 83, a renaming declaration like the above must appear in the specification, not the body.  In Ada 95, the specification can say simply procedure Print(Object : in String);, and the renames declaration above may appear in the body.

We can also rename task entries as procedures.  This is the only way to avoid the dot notation when calling a task entry.

A function can be renamed as an infix operator, if it has the right number and types of parameters.  Also, an infix operator can be renamed as a function.  For example, earlier we defined type Vector and wrote:

function "*"(Left, Right : in Vector) return Float;
This could be renamed as follows:
function Dot_Product(X, Y : in Vector) return Float renames "*";

Renaming can get around the restriction that library subprograms can't be infix operators.  We can use a normal function name for the library, and rename it as an infix operator for our program.  Similarly, we can get around the rule that library subprograms can't overload each other.  We can give subprograms distinct names in the library, and rename them in our program to overload each other.

An attribute that takes a parameter, such as 'Pred and 'Succ, can be renamed as a function.  For example,

function Next(M : in Month_Type) return Month_Type
   renames Month_Type'Succ;

Record components can be renamed.  If D is of type Date, we can write

Y : Integer renames D.Year;

Exceptions can also be renamed.  For example,

No_Such_File : exception renames Ada.Text_IO.Name_Error;
The ability to rename exceptions is used by Ada.Text_IO, Ada.Sequential_IO, Ada.Direct_IO, etc.  Earlier, when we gave a simplified specification of Ada.Text_IO, we pretended that the exceptions Status_Error, Mode_Error, End_Error, etc. were defined in that package.  Actually, they're defined in a package Ada.IO_Exceptions.  Ada.Text_IO withs that package, and declares
Status_Error : exception renames IO_Exceptions.Status_Error;
Mode_Error   : exception renames IO_Exceptions.Mode_Error;
etc.  Other packages like Ada.Direct_IO and Ada.Sequential_IO do exactly the same thing.  Suppose Random_IO is an instantiation of Ada.Direct_IO.  Then Ada.Text_IO.File_Type and Random_IO.File_Type are two different types, but Ada.Text_IO.Status_Error and Random_IO.Status_Error are the same exception, because they both rename Ada.IO_Exceptions.Status_Error.

A subtype can be used to achieve the effect of renaming a type.  For example,

subtype File is Ada.Text_IO.File_Type;

< prev   next >

No comments: