3.6.1. Package Masking: Renames and news - JulTob/Ada GitHub Wiki

Renaming

Renaming in Ada is a feature that allows you to create an alias for a package, subprogram, or any other entity. This can make your code more readable and manage complexity by providing shorter or more meaningful names for entities defined in other packages or libraries.

-- Renaming packages for easier access and improved readability.
package T_IO renames Ada.Text_IO;          -- Alias for the Text_IO package
package ACL renames Ada.Command_Line;      -- Alias for the Command_Line package

Using renames and new for Type Masking

While renames provides a straightforward alias, using new with generics is slightly different. It involves creating a new instance of a generic package tailored to specific data types or parameters. This is not just renaming but actually creating a new configuration of a generic package.

-- Instantiating a generic package for Integer operations
package I_IO is new Ada.Text_IO.Integer_IO(Integer);

This instantiation allows you to use the generic package (Ada.Text_IO.Integer_IO) with the Integer type, which effectively tailors the package's functionality to integers.

Initializing Generic Packages

Initializing a generic package involves specifying actual parameters for the placeholders defined in a generic package. This makes the package specific to a type or a set of operations.

Name-Initializing a generic is just producing a package for a specific, maybe self-made, type.

-- Defining a custom type with specific constraints
type Value_Type is digits 12 range
   -999_999_999_999.0e999 .. 999_999_999_999.0e999;

-- Instantiating a generic package for floating-point I/O operations tailored to Value_Type
package F_IO is new Ada.Text_IO.Float_IO(Value_Type);

This code defines a Value_Type with specific precision and range, and then creates a new instance of Ada.Text_IO.Float_IO that is customized to handle Value_Type.

Calling Methods from Renamed and Instantiated Packages

In the body of the code we can call methods inside packages in a way easier way.

Using renamed and instantiated packages simplifies the code by using the aliases and tailored instances, making the operations and calls within your program more readable.

   Value_1 : Value_Type; Value_2 : Value_Type;
  begin
   T_IO.Put ("First Value : ");
   F_IO.Get (Value_1);
   T_IO.Put ("Second Value : ");
   F_IO.Get (Value_2);

   F_IO.Put (Value_1);
   T_IO.Put (" + ");
   F_IO.Put (Value_2);
   T_IO.Put (" = ");
   F_IO.Put (Value_1 + Value_2);

Summary

  • Renaming provides a way to refer to existing packages or entities with a simpler or more relevant name in your specific application context.
  • Generic Instantiation with new allows the creation of specialized versions of generic packages or subprograms for particular types or configurations.
  • This approach reduces code complexity and enhances readability by abstracting away unnecessary details and focusing on the semantics relevant to your specific problem or domain.