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,4bc0e5c544f4d1eb X-Google-Attributes: gid103376,public From: mheaney@ni.net (Matthew Heaney) Subject: Re: question on variant record Date: 1996/11/03 Message-ID: #1/1 X-Deja-AN: 194122839 references: content-type: text/plain; charset=ISO-8859-1 organization: Estormza Software mime-version: 1.0 newsgroups: comp.lang.ada Date: 1996-11-03T00:00:00+00:00 List-Id: In article , michiel.perdeck@cmg.nl wrote: >Can the tag field in a variant record (the field that determines the >actual type of the record) be queried just like a normal field? It >makes sense that it cannot be set afterwards but one should want to >request it's actual value, I suppose. The technical term is "discriminant," not "tag field." And technically, a "variant record" is a discriminant record only with a variant part (case statement). You probably meant "discriminant record" in your subject line. Yes, you can always query the value of the discriminant, even for discriminated private types. For example, type Sequence (Size : Positive) is private; ... The_Sequence : Sequence (132); The_Size : constant Positive := The_Sequence.Size; ... The thing about discriminant records is whether or not you want objects of the type to be mutable (able to change the value of the discriminant) or to be a component of a composite type (an array or another record). A discriminated type without a default descriminant is "indefinate"; you have to specify a value of the discriminant when declaring objects of the type, and the object is then constrained to that discriminant value. The type Sequence above is an example. If the discriminated type has a default discriminant, and you declare the object without a discriminant value, then the object is mutable, and you can change its discriminant value. For example, type T_Kind is (T1, T2, T3); type T (Kind : T_Kind := T_Kind'First) is record ...; O : T; begin O := (T1, ...); O := (T2, ...); O := (T3, ...); However, if you declare the object with a descriminant, the object is constrained to that value, even if the type has a default: O : T (T2); begin O := (T3, ...); -- illegal One technique is to use the discriminant of the record as an index constraint of an array component. This is one way of making ragged arrays in Ada: subtype String_Buffer_Length_Range is Natural range 0 .. 80; type String_Buffer (Length : String_Buffer_Length_Range := 0) is record Text : String (1 .. Length); end record; type String_Buffer_Array is array (Positive range <>) of String_Buffer; function "+" (S : String) return String_Buffer is begin return (S'Length, S); -- in Ada 83, make sure you slide S end; SB : String_Buffer_Array (1 .. 10); begin SB (1) := +"hello"; SB (2) := +"goodbye"; SB (3) := +"Ada Lovelace"; SB (4) := +"Charles Babbage"; Of course, in all these examples, you can always read the value of the discriminant. -------------------------------------------------------------------- Matthew Heaney Software Development Consultant mheaney@ni.net (818) 985-1271