comp.lang.ada
 help / color / mirror / Atom feed
From: emwson@emw.ericsson.se (Martin Lorentzon)
Subject: Re: parameterless generics?
Date: 1996/07/18
Date: 1996-07-18T00:00:00+00:00	[thread overview]
Message-ID: <vmu3v6cas6.fsf@hera.emw.ericsson.se> (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?

How about getting the accessibility level right when extending tagged
types?

A small example:

======================================================================
-- This package is not generic because it needn't be.
package Foo is

   type Root is tagged null record;

end Foo;
======================================================================
-- This package needs to be generic for some reason.
with Foo;

generic
   type Element is private;

package Foobar is
   type Child is new Foo.Root with null record;
end Foobar;
======================================================================
-- Now we can't instansitate the foobar package. 
-- Compiling the `bar' procedure is an error:
with Foo;
with Foobar;
procedure Bar is 

   package Fb is new Foobar(Float);

begin
   null;
end Bar;
======================================================================
Rewriting the `Foo' package as a generic package solves the problem:
======================================================================
-- Now the package Foo is generic and takes no parameters. It's
-- generic only to used as a parameter to foobar:
generic
package Foo is

   type Root is tagged null record;

end Foo;
======================================================================
-- Package Foobar now takes an instantiation of package Foo as a
-- generic parameter to avoid getting the type extension at a deeper
-- accessibility level than the parent:
with Foo;

generic
   type Element is private;
   with package Foo_Inst is new Foo;
package Foobar is
   type Child is new Foo_Inst.Root with null record;
end Foobar;
======================================================================
-- Finally we let the `Bar' procedure take care of the instantiations:
with Foo;
with Foobar;
procedure Bar is 

   package F is new Foo;
   package Fb is new Foobar(Float,F);

begin
   null;
end Bar;


-- Regards,
--		Martin.




      parent reply	other threads:[~1996-07-18  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 ` Theodore E. Dennison
1996-07-12  0:00 ` Robert Dewar
1996-07-22  0:00   ` Mats Weber
1996-07-12  0:00 ` Philip Brashear
1996-07-14  0:00 ` Norman H. Cohen
1996-07-15  0:00 ` Chris Papademetrious
1996-07-17  0:00   ` Richard Riehle
1996-07-18  0:00 ` Martin Lorentzon [this message]
replies disabled

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