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.3 required=5.0 tests=BAYES_00,INVALID_MSGID autolearn=no autolearn_force=no version=3.4.4 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 103376,22ea9f82aa6070da X-Google-Attributes: gid103376,public From: "Matthew Heaney" Subject: Re: Variant record question Date: 2000/01/21 Message-ID: #1/1 X-Deja-AN: 575845544 Content-transfer-encoding: 7bit References: <4V0i4.622$dw3.29725@news.wenet.net> Content-Type: text/plain; charset="US-ASCII" X-ELN-Date: Fri Jan 21 11:32:35 2000 X-Complaints-To: abuse@earthlink.net X-Trace: newsread1.prod.itd.earthlink.net 948483155 38.26.88.85 (Fri, 21 Jan 2000 11:32:35 PST) Organization: EarthLink Network, Inc. Mime-version: 1.0 NNTP-Posting-Date: Fri, 21 Jan 2000 11:32:35 PST Newsgroups: comp.lang.ada Date: 2000-01-21T00:00:00+00:00 List-Id: In article <4V0i4.622$dw3.29725@news.wenet.net> , "Mike Silva" wrote: > I've been working on some proof-of-concept code, and I don't understand > something about arrays of variant records. Specifically, the following code > works with a default discriminant defined for the type T_t, but if I remove > the default discriminant it fails at the declaration of T_array_t. Why must > I put in a default discriminant when I can then override it in an aggregate > (variable 'A' below)? Thanks for any clarification. An array component subtype must be "definite." What you're doing is analogous to declaring an array of string: type String_Array is array (Positive range <>) of String; --illegal If I were to declare an array object: SA : String_Array; then how would the compiler know how much space to allocate for each array component? It wouldn't, that's why an array component subtype must be "definite." In this example, the array component subtype is itself an array subtype, which must be "constrained" in order to qualify as "definite": type String_Array is array (Positive range <>) of String (1 .. 10); Now the compiler knows how much space each array component takes up. You array of discriminated record type has the same problem. You have to tell the compiler how much space to allocate for each array component. In the array of variant record case, here's where the subtle difference between "constrained" and "definite" becomes clear. A variant record type with a default discriminant is "definite" and "unconstrained." It's "definite" because the compiler allocates enough space to store a record having any discriminant value. Because the compiler knows how much space this is, you're allowed to use it as the component subtype of an array declaration. However, unlike a constrained array (as in our array of string example above), a variant record declared with a default discriminant is unconstrained, which means you're free to change the discriminant value of objects of the type. Basically, the compiler needs to know how much space to allocate for array components. This means the array component subtype must be "definite." However, in the former example, the array component subtype was "constrained," and in the latter example, the array component subtype was "unconstrained." > package Test is > > type Dis_t is ( D1, D2 ); > > type T_t ( D : Dis_t := D1 ) is -- note default discriminant > record > case D is > when D1 => > I : Integer; > when D2 => > F : Float; > end case; > end record; > > type T_array_t is array (Integer range <>) of T_t; -- fails if no default > Dis_t Yes, it fails because a discriminated record type without a discriminant is "indefinite," which means the compiler doesn't know how much space to allocate for array components. > A : T_array_t := ( (D => D1, I => 99), (D => D2, F => 99.99) ); > > end Test; -- Help keep evolution in the science classroom and religion out: become a member of the National Center for Science Education.