comp.lang.ada
 help / color / mirror / Atom feed
From: bglbv@my-dejanews.com
Subject: Re: Can I have an array of variant records?
Date: 1999/04/20
Date: 1999-04-20T00:00:00+00:00	[thread overview]
Message-ID: <87aew3j899.fsf@bglbv.my-dejanews.com> (raw)
In-Reply-To: 371c84fb.0@silver.truman.edu

"Josh Highley" <joshhighley@hotmail.com> writes:

> I have the following type declaration and I would like to have an array of
> them.  However, I need "length" to have different values for each element of
> the array.
> 
> type field (length : positive) is limited private;

3.6(10) says that "the subtype defined by the subtype_indication of a
component_definition (the component subtype) shall be a definite subtype."

So you have to make up your mind as to the value of the discriminant
when you declare your array.

> --it's later defined to be a record with a string(1..length), among other
> information.
> 
> For instance, I would like field_array(1) to be a field with a length of 10,
> field_array(2) to be a field of length 50, and so on.  Can I declare an
> array of these fields, with each field of a different length?

Not that I can see, but you could make an array of access values instead.

Alternatively, you could get rid of the discriminant by using
something like a bounded string of a suitably chosen maximum length.
Or by making your record contain an access-to-String field instead
of a String: since the type is limited, you don't have to worry about
users of the package getting mixed up between shallow and deep copies.
(Otherwise you might have needed to make the type controlled.)

I find Ada's rules reasonably intuitive if I adopt a model in which
all components of an array must have the same storage size. In the
case of a record with a component of type String(1..Length), the
storage size of the record will depend on the value of Length (unless
the implementation decides to make that component a hidden pointer,
which is something I wouldn't want it to be forced to). So if you
want to make an array of these things, you'd better somehow arrange
for all components of the array to have the same value of the Length
discriminant. Or else sweep the size variability under the rug by
adding a level of indirection through access values. And if I had
to choose, I'd rather do that under cover of the "limited private",
i.e. by resorting to

   type String_Access is access String;
   type Field is record                  -- Note: no discriminant 
      ...
      Label : String_Access;             -- (the length is stored here)
      ...
   end record;

rather than to

   type Field_Access is access Field;
   type Field_Array is array (Index_Type range <>) of Field_Access;

But either way will "work" in some sense.




  reply	other threads:[~1999-04-20  0:00 UTC|newest]

Thread overview: 17+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
1999-04-20  0:00 Can I have an array of variant records? Josh Highley
1999-04-20  0:00 ` bglbv [this message]
1999-04-21  0:00   ` Robert Dewar
1999-04-21  0:00     ` bglbv
1999-04-22  0:00       ` dennison
1999-04-21  0:00     ` Pawel Kobylarz
1999-04-21  0:00       ` dennison
1999-04-21  0:00         ` Robert Dewar
1999-04-22  0:00           ` Josh Highley
1999-04-23  0:00           ` Pawel Kobylarz
1999-04-21  0:00       ` David C. Hoos, Sr.
1999-04-21  0:00         ` Robert Dewar
1999-04-21  0:00       ` czgrr
1999-04-21  0:00 ` Robert Dewar
1999-04-22  0:00 ` Maybe not exactly a variant record Josh Highley
1999-04-22  0:00   ` dennison
1999-04-23  0:00     ` Nick Roberts
replies disabled

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