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=-0.3 required=5.0 tests=BAYES_00, REPLYTO_WITHOUT_TO_CC autolearn=no 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!news2.google.com!news.germany.com!news.belwue.de!newsfeed.arcor.de!news.arcor.de!not-for-mail From: "Dmitry A. Kazakov" Subject: Re: Question about generics. Newsgroups: comp.lang.ada User-Agent: 40tude_Dialog/2.0.15.1 MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Reply-To: mailbox@dmitry-kazakov.de Organization: cbb software GmbH References: Date: Wed, 5 Jul 2006 14:08:08 +0200 Message-ID: NNTP-Posting-Date: 05 Jul 2006 14:08:08 MEST NNTP-Posting-Host: 6d319966.newsread4.arcor-online.net X-Trace: DXC=6a81=Kbn_:2E][lTbkER26:ejgIfPPld4jW\KbG]kaM8LL]:kiR8f=?kcA]]:gn:h2[6LHn;2LCV>7enW;^6ZC`4IXm65S@:3>? X-Complaints-To: usenet-abuse@arcor.de Xref: g2news2.google.com comp.lang.ada:5504 Date: 2006-07-05T14:08:08+02:00 List-Id: On Tue, 04 Jul 2006 09:29:40 -0400, Stephen Leake wrote: > 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. It is more interesting for cases unrelated to generics: User-defined static functions: function Factorial (N : static Natural) return static Natural; type My_Strange_Type is mod Factorial (5); function C_Compiler (X : static String) return static Float; C : constant Float := C_Compiler ("/* some C program */ float x=2.0; for(;;){..."); -- Evaluated by the compiler at compile-time. function Number_Quarks_Requred_On_This_Mashine ( OS : static String; CPU : static String; ... ) return static Quark_Number; function To_UTF8 (X : static String) return static String; while Current_Line = To_UTF8 ("...") loop ...; -- Manual constant folding is always possible, though end loop; type EEPROM is array (...) of Storage_Elements; Boot : static EEPROM := Link (C_Compiler (...)); for Boot'Address use ...; if Pure were a qualifier instead of pragma, then static result could be made implied from static parameters. I have difficulty to classify "static" as a type qualifier. Is static X is subtype, new type or what. Dimensioned arithmetic: type Unit is mod 2**28; -- Encoded powers of base units type Dimensioned (SI : Unit) record Gain : Float; end record; function "+" (Left, Right : Dimensioned) return Dimensioned; function "-" (Left, Right : Dimensioned) return Dimensioned; function "*" (Left, Right : Dimensioned) return Dimensioned; function "/" (Left, Right : Dimensioned) return Dimensioned; ... type Static_Dimensioned (SI : static Unit) is new Dimensioned (SI); -- Statically constrained Dimensioned, the compiler is *required* to -- drop the discriminant in the representation of. When converted -- back to Dimensioned, the discriminant is reconstructed. Forward -- conversion is compile-time type error, when the discriminant -- isn't static. function "+" (Left, Right : Static_Dimensioned) return Static_Dimensioned; function "-" (Left, Right : Static_Dimensioned) return Static_Dimensioned; function "*" (Left, Right : Static_Dimensioned) return Static_Dimensioned; function "/" (Left, Right : Static_Dimensioned) return Static_Dimensioned; ... I think you've got the idea. This gives you dimensioned arithmetic fully statically checkable, space/time penalty free, which also can handle unknown dimensions at run-time. But there are too many problems with this. Note how important are partially static types like Static_Dimensioned. Just one type qualifier "static" does not solve the problem. It must be much finer. -- Regards, Dmitry A. Kazakov http://www.dmitry-kazakov.de