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.9 required=5.0 tests=BAYES_00 autolearn=ham autolearn_force=no version=3.4.4 X-Google-Thread: 103376,878cc452376823e0 X-Google-Attributes: gid103376,domainid0,public,usenet X-Google-Language: ENGLISH,ASCII Path: g2news1.google.com!postnews.google.com!u36g2000prf.googlegroups.com!not-for-mail From: Adam Beneschan Newsgroups: comp.lang.ada Subject: Re: Generic body Date: Wed, 14 May 2008 15:03:31 -0700 (PDT) Organization: http://groups.google.com Message-ID: References: NNTP-Posting-Host: 66.126.103.122 Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable X-Trace: posting.google.com 1210802611 3801 127.0.0.1 (14 May 2008 22:03:31 GMT) X-Complaints-To: groups-abuse@google.com NNTP-Posting-Date: Wed, 14 May 2008 22:03:31 +0000 (UTC) Complaints-To: groups-abuse@google.com Injection-Info: u36g2000prf.googlegroups.com; posting-host=66.126.103.122; posting-account=duW0ogkAAABjRdnxgLGXDfna0Gc6XqmQ User-Agent: G2/1.0 X-HTTP-UserAgent: Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.7.12) Gecko/20050922 Fedora/1.7.12-1.3.1,gzip(gfe),gzip(gfe) Xref: g2news1.google.com comp.lang.ada:68 Date: 2008-05-14T15:03:31-07:00 List-Id: On May 14, 11:26 am, S=E9bastien wrote: > 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 =3D> Integer); > procedure MyProcStr is new MyProc(Toto =3D> String); This one can't be right in any case, because String is an unconstrained type. You can't declare a *variable* X : String; so therefore you can't use String as the actual type for Toto, because MyProc could declare a variable X : Toto; 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. > > end MyPack; > > package body MyPack is > > procedure MyProc is > begin > -- Some stuff > end MyProc; > > end MyPack; 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. 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 =3D> Integer); procedure MyProcStr is new MyProcPack.MyProc(Toto =3D> 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 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. -- Adam