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!news4.google.com!border1.nntp.dca.giganews.com!nntp.giganews.com!wns13feed!worldnet.att.net!attbi_s22.POSTED!53ab2750!not-for-mail From: "Jeffrey R. Carter" Organization: jrcarter at acm dot org User-Agent: Thunderbird 1.5 (Windows/20051201) MIME-Version: 1.0 Newsgroups: comp.lang.ada Subject: Re: Question about generics. References: In-Reply-To: Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit Message-ID: <7Q2qg.812592$084.507058@attbi_s22> NNTP-Posting-Host: 12.201.97.176 X-Complaints-To: abuse@mchsi.com X-Trace: attbi_s22 1151908227 12.201.97.176 (Mon, 03 Jul 2006 06:30:27 GMT) NNTP-Posting-Date: Mon, 03 Jul 2006 06:30:27 GMT Date: Mon, 03 Jul 2006 06:30:27 GMT Xref: g2news2.google.com comp.lang.ada:5423 Date: 2006-07-03T06:30:27+00:00 List-Id: Peter C. Chapin wrote: > > generic > Size : in Integer; > package Xyzzy is > type Index is mod Size; > -- etc. > end Xyzzy; What does a Size of -7 mean? As you've discovered, generic formal parameters are not static in Ada, and so cannot be used in a type declaration. You can do generic type Index is mod <>; package P is Size : constant Positive := Index'Modulus; end P; > 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. Except there's no way to do that. > template< int size > > class Xyzzy { > char array[size]; // size is a compile time constant. > }; This is a different kettle of fish. You can't declare new integer types in C++, so you can't compare what you're trying to do with C++. Array subtype constraints don't have to be static, or even constant: generic Size : Positive; -- or Natural, if zero is acceptable package P is subtype S is String (1 .. Size); -- Size is a non-static constant end P; declare N : Positive := Get; begin if N rem 2 /= 0 then N := N + 1; end if; declare subtype S is String (1 .. N); begin null; end; end; Note that Integer is perfectly OK for using type String, for exponents, and for sizes and counts that will fit in a 16-bit signed integer, but for just about everything else you should be using user-defined types based on the requirements of the application. Whichever you use, if negative values are not meaningful, you should explicitly exclude them by using an appropriate subtype. -- Jeff Carter "We'll make Rock Ridge think it's a chicken that got caught in a tractor's nuts!" Blazing Saddles 87