comp.lang.ada
 help / color / mirror / Atom feed
* Variant record question
@ 2000-01-21  0:00 Mike Silva
  2000-01-21  0:00 ` Robert A Duff
                   ` (2 more replies)
  0 siblings, 3 replies; 4+ messages in thread
From: Mike Silva @ 2000-01-21  0:00 UTC (permalink / raw)


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.

Mike

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

 A : T_array_t := ( (D => D1, I => 99), (D => D2, F => 99.99) );

end Test;









^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: Variant record question
  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
  2000-01-21  0:00 ` Ted Dennison
  2 siblings, 0 replies; 4+ messages in thread
From: Matthew Heaney @ 2000-01-21  0:00 UTC (permalink / raw)


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/>





^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: Variant record question
  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
@ 2000-01-21  0:00 ` Ted Dennison
  2 siblings, 0 replies; 4+ messages in thread
From: Ted Dennison @ 2000-01-21  0:00 UTC (permalink / raw)


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)?

I have personally never written an Ada compiler, but below is roughly
the dialog that I imagine is going on between a developer and the
compiler when a discriminated type is declared. Hopefully the
experienced compiler writers in the crowd will correct anything that I
got a bit wrong.

When you declare a discriminated type without defaults for its
discriminants, you are effectively telling the compiler: "I'll tell you
how big objects of this type are when I create them." When the compiler
needs to know the objects' size (eg, so it can write the machine code to
perform an assignment) It may need to keep an associated dope record
with the size, or look at the discriminant and do a calculation.

Suppose I want an array of these things. Now suppose I come along and
ask for element 20. How does the compiler know where element 2000 is? It
would have to look up the sizes of every element from 1 to 1999. That's
not exactly the kind of effencicy we'd typically want out of an array!
That kind of indexing is what records are for.

When you give the type a default discriminant, you are effectively
telling the compiler: "I don't know how big objects of this type are
either; I may need to change it. So please just allocate enough space
for the maximum that my discriminant values could ever require. And
until I tell you otherwise, use this discriminant value."

Now since every object of this discriminated type is the same size (the
maximum it could ever need to be), array indexing can work just like it
would with any other object.

--
T.E.D.

http://www.telepath.com/~dennison/Ted/TED.html


Sent via Deja.com http://www.deja.com/
Before you buy.




^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: Variant record question
  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
  2000-01-21  0:00 ` Ted Dennison
  2 siblings, 0 replies; 4+ messages in thread
From: Robert A Duff @ 2000-01-21  0:00 UTC (permalink / raw)


"Mike Silva" <mjsilva@jps.net> writes:

>...Why must
> I put in a default discriminant when I can then override it in an aggregate
> (variable 'A' below)?

It was a design goal of Ada 83 that it should be impossible to have an
uninitialized discriminant.  Therefore, if you don't want to give a
value when you create each object, you have to have a default.

Also, having a default means that there can exist objects of the type
whose discriminant can change (unless the record is limited).  That's
weird, IMHO -- there should have been a separate syntax for that.  This
rule always seems to confuse people.

It was also a dresign goal that array components should always have the
same size.

- Bob




^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2000-01-21  0:00 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
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
2000-01-21  0:00 ` Ted Dennison

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