From: Sébastien <seb.morand@gmail.com>
Subject: Re: Generic body
Date: Thu, 15 May 2008 09:25:12 +0000
Date: 2008-05-15T09:25:12+00:00 [thread overview]
Message-ID: <482C0178.5020501@gmail.com> (raw)
In-Reply-To:
> somewhere in it. But if you really need the ability to use "String"
> here, do this in your generic:
>
> type Toto(<>) is limited private;
>
> I realize this has nothing to do with your question.
Not but it's an interesting issue. I remembered this syntax I saw some
places and then read that:
http://en.wikibooks.org/wiki/Ada_Programming/Generics#Generic_formal_types
Now I understand well the matter. Thanks.
> Regarding your question: you really can't do this in Ada, because of
> 3.11(13-14): "For the instantiation of a generic unit that has a body,
> a check is made that this body is already elaborated....The exception
> Program_Error is raised if any of these checks fails." The compiler
> *should* accept your program with no errors, but it should fail at
> runtime because the program will have to "elaborate" the generic
> instantiation before it elaborates the generic body, and the program
> will fail on Program_Error before it even gets started. If your
> compiler is rejecting your program, then either it is doing so because
> it "knows" that the program can't possibly work when it runs, or it
> has a bug.
> Although there may be ways to suppress the elaboration check,
> depending on your compiler, I wouldn't take that approach, because the
> check is there for a reason. If you suppress the check, there's a
> possibility that your program may not work as expected, depending on
> what the body of your generic looks like, especially if it refers to
> any global variables in the body of MyPack.
Yes I understand that warning are now a good things in Ada :-) Espcially
when it says some exceptions will be raised at runtime. So I take time
to undestand and correct any warning.
> I'd recommend putting the generic in its own library unit, or in a
> different package:
>
> package MyProcPack is -- I hate this name
> generic
> type Toto(<>) is limited private;
> procedure MyProc;
> end MyProcPack;
>
> package body MyProcPack is
> procedure MyProc is ... end MyProc;
> end MyProcPack;
>
> with MyProcPack;
> pragma Elaborate_All (MyProcPack);
> package MyPack is
> procedure MyProcInt is new MyProcPack.MyProc(Toto => Integer);
> procedure MyProcStr is new MyProcPack.MyProc(Toto => String);
> end MyPack;
>
> The Elaborate_All pragma is necessary to ensure that the body of
> MyProcPack (including the body of the generic MyProc) is elaborated
> before MyProc is instantiated.
>
> If you don't understand what elaboration is all about, it's too
> difficult to explain here---sorry. But just trust me that you'll need
> to do something like what I'm recommending.
I read a lot of stuff about elaboration, I'm not a master of the subject
yet, but I understand a bit about it. I thought that Elaborate_All was
about dynamic check vs static check? (flat -gnatE for circular dependancy)
>> I don't how to do this because:
>> 1) The compiler refuse the instance of MyProc because it doesn't have
>> the MyProc body
>
> As I've mentioned, the compiler really shouldn't refuse this, because
> it's a legal Ada program although it cannot work right. Some
> compilers have modes that cause them to reject programs that are
> certain to raise predefined exceptions, even when the programs are
> legal, and your compiler could be running in a mode like this.
>
>> 2) If I put the body of MyProc in the package spec, the compiler refuses
>> it because it's not the right place.
>
> Right---you can't put any sort of body in a package spec.
Ok thanks to all of you for your help.
Sebastien
next prev parent reply other threads:[~2008-05-15 9:25 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
2008-05-14 21:51 ` Samuel Tardieu
2008-05-14 22:03 ` Adam Beneschan
2008-05-15 9:25 ` Sébastien [this message]
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