1

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?

j.Shry
  • 29
  • 5
  • 1
    You can instantiate a generic package as many times as you want in a single program, with different generic parameters such as different types. Each has its own name so there should be no ambiguity. –  Nov 14 '16 at 22:35
  • But let's say I have Ada.Integer_Text_IO; through the entirety of the program, how would that be fixed to do either integer or float – j.Shry Nov 14 '16 at 22:45
  • Ada.Float_Text_IO would do float. You can "with" both. Then Put(2.0) invokes one function while Put(2) invokes another. –  Nov 14 '16 at 22:53
  • I have to use the full library name – j.Shry Nov 14 '16 at 23:13
  • Ada.Integer_Text_IO or Ada.FLoat_Text_IO; – j.Shry Nov 14 '16 at 23:13
  • have you tried `use` clauses? –  Nov 14 '16 at 23:56
  • Another option is to have the client of the generic package pass a procedure as a generic parameter. That procedure should take an instance of your generic element type and produce the appropriate output. It would be up to the client to define the formatting of the output, and enforce that formatting in the procedure passed as a generic parameter. For instance, if the name of the generic procedure is Print and the generic element is Element_Type then the generic procedure parameter would be **with procedure Print(Item : in Element_Type);** – Jim Rogers Nov 15 '16 at 00:41

2 Answers2

1

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;
Keith M
  • 853
  • 10
  • 28
Jim Rogers
  • 4,822
  • 1
  • 11
  • 24
0

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;
B98
  • 1,229
  • 12
  • 20