comp.lang.ada
 help / color / mirror / Atom feed
From: "Randy Brukardt" <randy@rrsoftware.com>
Subject: Re: Generic body
Date: Wed, 14 May 2008 16:49:48 -0500
Date: 2008-05-14T16:49:48-05:00	[thread overview]
Message-ID: <g0fmqu$g0f$1@jacob-sparre.dk> (raw)
In-Reply-To: g0fash$73v$1@registered.motzarella.org

[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #1: Type: text/plain, Size: 1661 bytes --]

"S�bastien" <seb.morand@gmail.com> wrote in message 
news:g0fash$73v$1@registered.motzarella.org...
> Hi,
>
> I'm trying to create a generic procedure with some instance in the same 
> package.
>
> package MyPack is
>
>   generic
>     type Toto is limited private;
>   procedure MyProc;
>
>   procedure MyProcInt is new MyProc(Toto => Integer);
>   procedure MyProcStr is new MyProc(Toto => String);
>
> end MyPack;
>
> package body MyPack is
>
>   procedure MyProc is
>   begin
>     -- Some stuff
>   end MyProc;
>
> end MyPack;
>
> I don't how to do this because:
> 1) The compiler refuse the instance of MyProc because it doesn't have the 
> MyProc body
> 2) If I put the body of MyProc in the package spec, the compiler refuses 
> it because it's not the right place.
>
> Thanks by advance for any help

You can't do this. The language doesn't allow it because the body of the 
instances would have to be elaborated before the body of the generic unit. 
That wouldn't be a major problem for a subprogram, but for a generic 
package, that could lead to all kinds of havoc (specifically, accessing 
objects that don't yet exist). To be consistent, it raises Program_Error in 
all cases. (GNAT rejects some of these cases at compile-time by default, 
which is not standard Ada behavior. But it still wouldn't work.) And, as 
you've found out, the only way to put a body into a specification is via an 
instance.

You'll have to put the instances in a separate unit from the generic unit. 
That's usually preferable anyway (especially in larger projects), but you 
don't have any choice in this case.

                                            Randy.





  reply	other threads:[~2008-05-14 21:49 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2008-05-14 18:26 Generic body Sébastien
2008-05-14 21:49 ` Randy Brukardt [this message]
2008-05-14 21:51 ` Samuel Tardieu
2008-05-14 22:03 ` Adam Beneschan
2008-05-15  9:25   ` Sébastien
2008-05-15 14:59     ` Adam Beneschan
2008-05-15 15:40       ` Sébastien
2008-05-15  8:43 ` gautier_niouzes
2008-05-15 13:11   ` Sébastien
replies disabled

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