comp.lang.ada
 help / color / mirror / Atom feed
From: mheaney@ni.net (Matthew Heaney)
Subject: Re: question on variant record
Date: 1996/11/03
Date: 1996-11-03T00:00:00+00:00	[thread overview]
Message-ID: <mheaney-ya023180000311961124440001@news.ni.net> (raw)
In-Reply-To: E090qu.JpG@tip.nl


In article <E090qu.JpG@tip.nl>, 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




  reply	other threads:[~1996-11-03  0:00 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
1996-11-02  0:00 question on variant record Michiel Perdeck
1996-11-03  0:00 ` Matthew Heaney [this message]
1996-11-14  0:00   ` Robert Dewar
1996-11-15  0:00     ` Matthew Heaney
  -- strict thread matches above, loose matches on Subject: below --
1996-11-02  0:00 Michiel Perdeck
1996-11-03  0:00 ` David C. Hoos, Sr.
replies disabled

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox