comp.lang.ada
 help / color / mirror / Atom feed
From: ncohen@watson.ibm.com (Norman H. Cohen)
Subject: Re: parameterless generics?
Date: 1996/07/14
Date: 1996-07-14T00:00:00+00:00	[thread overview]
Message-ID: <4sb7om$ful@watnews1.watson.ibm.com> (raw)
In-Reply-To: 4s48k9$3be$1@mhafn.production.compuserve.com


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




  parent reply	other threads:[~1996-07-14  0:00 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
1996-07-12  0:00 parameterless generics? Brian Gilbert
1996-07-11  0:00 ` Ray Blaak
1996-07-15  0:00   ` Mark A Biggar
1996-07-11  0:00 ` Laurent Guerby
1996-07-12  0:00 ` Robert Dewar
1996-07-22  0:00   ` Mats Weber
1996-07-12  0:00 ` Theodore E. Dennison
1996-07-12  0:00 ` Philip Brashear
1996-07-14  0:00 ` Norman H. Cohen [this message]
1996-07-15  0:00 ` Chris Papademetrious
1996-07-17  0:00   ` Richard Riehle
1996-07-18  0:00 ` Martin Lorentzon
replies disabled

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox