- Fri 10 July 2015
- misc
- Tero Koskinen
- #generics
There are some cases when you want to create and use multiple instances of same package.
This is easily done by creating a generic package which does not take any parameters:
generic package Counter is function Get_Number return Natural; end Counter; package body Counter is Counter_Value : Natural := 0; function Get_Number return Natural is Return_Value : constant Natural := Counter_Value; begin Counter_Value := Counter_Value + 1; return Return_Value; end Get_Number; end Counter;
Then you use package like this:
with Ada.Text_IO; with Counter; procedure Main is use Ada.Text_IO; package Counter_1 is new Counter; package Counter_2 is new Counter; begin Put_Line (Natural'Image (Counter_1.Get_Number)); Put_Line (Natural'Image (Counter_1.Get_Number)); Put_Line (Natural'Image (Counter_2.Get_Number)); end Main;
The output is:
0 1 0