comp.lang.ada
 help / color / mirror / Atom feed
From: "Matthew Heaney" <matthew_heaney@acm.org>
Subject: Re: Variant record question
Date: 2000/01/21
Date: 2000-01-21T00:00:00+00:00	[thread overview]
Message-ID: <nv2i4.1920$OT5.106122@newsread1.prod.itd.earthlink.net> (raw)
In-Reply-To: 4V0i4.622$dw3.29725@news.wenet.net

In article <4V0i4.622$dw3.29725@news.wenet.net> , "Mike Silva" 
<mjsilva@jps.net> 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.

<http://www.natcenscied.org/>





  parent reply	other threads:[~2000-01-21  0:00 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2000-01-21  0:00 Variant record question Mike Silva
2000-01-21  0:00 ` Robert A Duff
2000-01-21  0:00 ` Matthew Heaney [this message]
2000-01-21  0:00 ` Ted Dennison
replies disabled

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