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,c022fc5445abd13d X-Google-Attributes: gid103376,public X-Google-Language: ENGLISH,ASCII-7-bit Path: g2news1.google.com!news3.google.com!news.glorb.com!news.astraweb.com!router1.astraweb.com!207.217.77.102.MISMATCH!elnk-nf2-pas!newsfeed.earthlink.net!stamper.news.pas.earthlink.net!newsread1.news.pas.earthlink.net.POSTED!a6202946!not-for-mail From: "Jeffrey R. Carter" Organization: jrcarter at acm dot org User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.7.12) Gecko/20050915 X-Accept-Language: en-us, en MIME-Version: 1.0 Newsgroups: comp.lang.ada Subject: Re: Discriminated types with default discriminants References: In-Reply-To: Content-Type: text/plain; charset=us-ascii; format=flowed Content-Transfer-Encoding: 7bit Message-ID: Date: Thu, 03 Nov 2005 18:51:32 GMT NNTP-Posting-Host: 67.3.212.114 X-Complaints-To: abuse@earthlink.net X-Trace: newsread1.news.pas.earthlink.net 1131043892 67.3.212.114 (Thu, 03 Nov 2005 10:51:32 PST) NNTP-Posting-Date: Thu, 03 Nov 2005 10:51:32 PST Xref: g2news1.google.com comp.lang.ada:6154 Date: 2005-11-03T18:51:32+00:00 List-Id: Maciej Sobczak wrote: > type Discriminated(Size : Integer := 10) is > record > Value : String (1..Size); > end record; Aside : begin When I see something like this, I always wonder: What does a negative Size mean? To my mind, a negative Size is meaningless, and should not be allowed: Size : Natural end Aside; There are 3 cases with discriminants: 1. No default (object must be declared with explicit value) 2. Default, and object declared with explicit value 3. Default, and object declared without explicit value The 1st 2 cases are equivalent. The object must always have the same value for the discriminant. It can never change. The object is constrained. The last case means the discriminant can change. The object is unconstrained. Given subtype V_Index is Natural range 0 .. 255; type V_String (Length : V_Index := 0) is record Data : String (1 .. Length) := String'(1 .. Length => 'J'); end record; V : V_String; one can later change the Length of V: V := V_String'(Length => 23, Data => String'(1 .. 23 => 'C') ); There are 2 ways the compiler may implement this. Many compilers, including GNAT, allocate enough storage for the largest value, and then use parts of this storage depending on the current value of the discriminant. If the largest value is too big, such a declaration results in Storage_Error being raised. A few compilers will add a level of indirection, and store the variable-sized stuff elsewhere, reallocating storage when the amount available is not big enough. I think RR Software's Janus Ada compiler does this. With this scheme, you only get Storage_Error if you actually try to store a value that is too big. As with most implementation decisions, it's a trade off among complexity, space, and time. -- Jeff Carter "Now go away or I shall taunt you a second time." Monty Python & the Holy Grail 07