j.Shry
j.Shry

Reputation: 19

In Ada generic packages

In Ada Generic packages if I have a package that deals with any element you put in, if I would want to demonstrate that the package is working, would I have to run two separate client programs to show Integer and then Float, or could I do all that in the same program?

Upvotes: 1

Views: 876

Answers (2)

B98
B98

Reputation: 1239

IIUC, the "inner" package is the one that, too, depends on the generic formal type of the outer package, at least as far as testing goes. Then there are two cases.

  1. If the inner package is a plain package, such as Integer_Text_IO, it can only handle singed integer types, and that's a compile time thing.

  2. The inner package is of a kind that can be got from instantiating, using the generic formal type of the outer generic package.

In the first case, there is nothing the compiler can do but reject, since Integer_Text_IO is not made for floating point operands. So you'd have to set up separate test cases.

In the second case, the outcome depends on the "inner instance". Since the compiler cannot create a generic package when given a type, it can only instantiate a generic package that exists. The latter one must have matching formal requirements. That is, the generic formal types of the outer generic and of the to-be-instantiated inner generic must match: they must not be from a mutually exclusive category, like range <> and digits <>.

Sometimes, it is worth considering that one can specify requirements for the "inner generic" by making it a formal parameter of the outer generic:

generic
    type X (<>) is limited private;
package Taking_Any is
    -- ... operations for both FPT and integer types
end Taking_Any;

generic
    type T is private;
    with package Works_With_Any is new Taking_Any (<>);
package Outer is
    package Any_Instance is new Taking_Any (T);
end Outer;

Upvotes: 0

Jim Rogers
Jim Rogers

Reputation: 5021

The generic parameters should include a generic procedure parameter for printing the generic data type passed to the package. This will allow the data type to be anything and the writer of the generic package need not be concerned with how it is output.

generic
   type element_type is private;
   with procedure Print(Item : element_type);
package gen_pack is
   ...
end gen_pack;

Upvotes: 1

Related Questions