From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: * X-Spam-Status: No, score=1.7 required=5.0 tests=BAYES_00,PDS_OTHER_BAD_TLD, REPLYTO_WITHOUT_TO_CC autolearn=no autolearn_force=no version=3.4.4 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 103376,30fad28eb8886cca X-Google-Attributes: gid103376,public From: ncohen@watson.ibm.com (Norman H. Cohen) Subject: Re: parameterless generics? Date: 1996/07/14 Message-ID: <4sb7om$ful@watnews1.watson.ibm.com> X-Deja-AN: 168818087 distribution: world references: <4s48k9$3be$1@mhafn.production.compuserve.com> organization: IBM T.J. Watson Research Center reply-to: ncohen@watson.ibm.com newsgroups: comp.lang.ada Date: 1996-07-14T00:00:00+00:00 List-Id: In article <4s48k9$3be$1@mhafn.production.compuserve.com>, Brian Gilbert <71413.1453@CompuServe.COM> writes: |> Does anybody have a reason to use a generic (package or |> subprogram) without a parameter? Several people pointed out that each instance gets its own copies of variables declared in the package, and someone pointed out that each instantiation can initiate its own tasks. In Ada 95, parameterless generics are useful as children of generic packages. Such children are themselves required to be generic (so that the instantiation names a particular instance of the parent template, in which the new instance of the child template will be logically nested). However, it is typically not necessary to specify additional generic parameters for the child. For example: generic type Element_Type is private; Capacity: in Positive; package Stacks is pragma Pure; type Stack_Type is private; procedure Push (Element: in Element_Type; Stack: in out Stack_Type); procedure Pop (Element: out Element_Type; Stack: in out Stack_Type); function Current_Size (Stack: Stack_Type) return Natural; Stack_Error: exception; private type Element_Array_Type is array (1 .. Capacity) of Element_Type; type Stack_Type is record Top : Integer range 0 .. Capacity := 0; Elements : Element_Array_Type; end record; end Stacks; generic procedure Stacks.Make_Empty (Stack: out Stack_Type); -- This child of Stacks is a parameterless generic procedure procedure Stacks.Make_Empty (Stack: out Stack_Type) is begin Stack.Top := 0; end Stacks.Make_Empty; ---------------------------------------------------------------------- with Ada.Strings.Unbounded, Stacks; package File_Name_Stacks is new Stacks (Ada.Strings.Unbounded.Unbounded_String, 1023); -- Within the scope of a with clause for Stacks.Make_Empty, the -- parent instance File_Name_Stacks can be viewed as logically -- containing a generic template named Make_Empty, in which -- global references to Stack_Type refer to -- File_Name_Stacks.Stack_Type. (The child template does not -- contain any references to Element_Type, but if it did, they -- would refer to Unbounded_String.) with File_Name_Stacks, Stacks.Make_Empty; procedure Make_File_Name_Stack_Empty is new File_Name_Stacks.Make_Empty; -- This instantiates the generic template logically contained in -- File_Name_Stacks. ---------------------------------------------------------------------- with Stacks; package Integer_Stacks is new Stacks (Integer, 8193); -- Within the scope of a with clause for Stacks.Make_Empty, the -- parent instance Integer_Stacks can be viewed as logically -- containing DIFFERENT a generic template named Make_Empty, in -- which global references to Stack_Type refer to -- Integer_Stacks.Stack_Type. (If the child template had -- contained any references to Element_Type, they would refer to -- Integer.) with Integer_Stacks, Stacks.Make_Empty; procedure Make_Integer_Stack_Empty is new Integer_Stacks.Make_Empty; -- This instantiates the generic template logically contained in -- Integer_Stacks. (See pages 731-732 of Ada as a Second Language for another example of a parameterless generic used in this way.) Of course a child of a generic is not PRECLUDED from having generic parameters. For example: with Ada.Text_IO; use Ada.Text_IO; generic with procedure Dump_Element (File: in File_Type; Element: in Element_Type); procedure Stacks.Dump_Stack (File: in File_Type; Stack: in Stack_Type); I've also seen unusual examples in which a generic package with an empty visible part is instantiated to "install" something during the execution of the initialization statements in the package body, but these examples generally have generic parameters. -- Norman H. Cohen ncohen@watson.ibm.com