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,cea03ed275aa3d28 X-Google-Attributes: gid103376,public X-Google-Language: ENGLISH,ASCII-7-bit Path: g2news2.google.com!news3.google.com!border1.nntp.dca.giganews.com!border2.nntp.dca.giganews.com!nntp.giganews.com!atl-c08.usenetserver.com!news.usenetserver.com!pc02.usenetserver.com!news.flashnewsgroups.com-b7.4zTQh5tI3A!not-for-mail Newsgroups: comp.lang.ada Subject: Re: Question about generics. References: From: Stephen Leake Date: Tue, 04 Jul 2006 09:29:40 -0400 Message-ID: User-Agent: Gnus/5.1006 (Gnus v5.10.6) Emacs/21.3 (windows-nt) Cancel-Lock: sha1:AwhNhukzn9rqr3b3lI9f9C6Lr2I= MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii X-Complaints-To: abuse@flashnewsgroups.com Organization: FlashNewsgroups.com X-Trace: 48a9544aa6d50e73ae4a425509 Xref: g2news2.google.com comp.lang.ada:5476 Date: 2006-07-04T09:29:40-04:00 List-Id: "Peter C. Chapin" writes: > Here is what I'm trying to do > > generic > Size : in Integer; > package Xyzzy is > type Index is mod Size; > -- etc. > end Xyzzy; > > The compiler (gnat) complains about the modular type definition, saying > that Size is a non-static expression and that's no good. I understand > what this error means and I understand the rationale behind it (thanks > to looking in Cohen's book "Ada As a Second Language"). For others, I'll summarize that rationale here. The generic above might be instantiated in a procedure: procedure Foo (User_Size : in Integer) is package Bar is new Xyzzy (Size => User_Size); begin ... end Foo; In this case, Size is _not_ static, not even in an informal sense. So the compiler doesn't know how many bits to use for Index. > My question is: how can I get the effect I'm looking for? My plan is > to only instantiate the package with constants so all the necessary > information should be available at compile time. For example > > package Fizzle is new Xyzzy(Size => 10); > > I realize that I could make the Index type a generic parameter but that > would require me to define the type at the point of instantiation and > that seems unnatural. Conceptually I'm trying to parameterize the > package on a (static) size. It doesn't seem like I should have to define > a type to express that concept. That does seem like a reasonable goal. But I think you need to modify the language to get there. Perhaps a new class of generic parameters: generic Size : static Integer; package Xyzzy_Static is type Index is mod Size; -- etc. end Xyzzy_Static; Then Xyzzy_Static could only be instantiated with truly static Size (in the Ada meaning of 'static'). Short of that, I think you have to declare the type outside the generic package, as you say. I often find it better to declare the type outside the package, because I want it for things the package doesn't declare. But if you intend to put everything into the generic, it would be nice to do it your way. It would be interesting to raise the 'static' proposal on the Ada comment mailing list (where they discuss future changes to the Ada language). But to do that right, I'd need more information about why you want to do this. Their mostly likely response will be "it's not hard to declare the type outside the package". So we need some more ammunition about why that's not a good answer. -- -- Stephe