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-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 103376,d534ed04fa87be8b X-Google-Attributes: gid103376,public X-Google-ArrivalTime: 2002-12-23 03:21:13 PST Path: archiver1.google.com!news1.google.com!newsfeed.stanford.edu!logbridge.uoregon.edu!fu-berlin.de!uni-berlin.de!dialin-145-254-040-099.arcor-ip.NET!not-for-mail From: "Dmitry A. Kazakov" Newsgroups: comp.lang.ada Subject: Re: Discriminant name must be alone Date: Mon, 23 Dec 2002 12:21:29 +0100 Organization: At home Message-ID: References: Reply-To: mailbox@dmitry-kazakov.de NNTP-Posting-Host: dialin-145-254-040-099.arcor-ip.net (145.254.40.99) Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7Bit X-Trace: fu-berlin.de 1040642472 5137689 145.254.40.99 (16 [77047]) User-Agent: KNode/0.7.1 Xref: archiver1.google.com comp.lang.ada:32228 Date: 2002-12-23T12:21:29+01:00 List-Id: Robert A Duff wrote: > However, I have run into a few cases where I would like to do what > you're suggesting here. For example, I wrote a "storage pool" type, > where the private part defined the storage as an array of machine words. > The allocated types were known to be always word aligned, and always a > multiple of word size. The array of words makes alignment easy, and the > Allocate procedure can simply return 'Address of one of the array > components. But I wanted the client to specify the size in > Storage_Elements. So the array would have to be: > > Storage: Word_Array(0..(Size_In_Bytes-1)/4); -- Illegal! > > where Size_In_Bytes is the discriminant. Or something like that. > > I think the main problem with allowing that (besides what the AARM > mentions) is that if you allow arbitrary expressions referring to > discriminants, you have to worry about when that expression is > evaluated. Suppose that expression is "F(Size_In_Bytes)", where F is > some function that might have side effects. If it has side effects, it > might return a different answer each time it's called. > > I suppose one answer is to say that the expression is evaluated whenever > a value of the record type is created. The value of that expression > must be saved as "dope" -- a hidden record component created by the > compiler. If it has side effects, that's the programmer's problem. > This would be the first case where a compiler is required to store > dope. Some compilers store dope, but it's never required. > > Another answer is, "forbid side effects in those expressions", so the > compiler can evaluate those expressions whenever it likes. Ada has no > such mechanism, currently. To provide such a mechanism, each function > would have to declare (on the spec) what variables it meddles with -- > as in SPARK. If the expression "Start_Ind + 10" is allowed, then surely > you should be allowed to call a function that does "return Start_Ind + > 10;". There is a third answer: "user-defined constructors". Should you have a user-defined constructor for a discriminated type, you could evaluate its discriminants during object construction, which would have same effect as if they were expressions (with the arguments provided by the constructor's argument list). User-defined constructors could also solve the "assignment problem", because an assignment can be easily generated out of copy-constructor. -- Regards, Dmitry A. Kazakov www.dmitry-kazakov.de