comp.lang.ada
 help / color / mirror / Atom feed
From: Georg Bauhaus <rm.tsoh.plus-bug.bauhaus@maps.futureapps.de>
Subject: Re: (Num_Types.Mod_4 range 1..8) ------->why not (1..8)?
Date: Mon, 18 May 2009 22:53:55 +0200
Date: 2009-05-18T22:53:56+02:00	[thread overview]
Message-ID: <4a11cae4$0$32667$9b4e6d93@newsspool2.arcor-online.net> (raw)
In-Reply-To: <6b5fe400-2564-4fc9-a5bf-f83d1e476345@q14g2000vbn.googlegroups.com>

convergence82 wrote:

> Thanks.  I'm looking at a Ada.Unchecked_Conversion function that
> converts from Data_Array_1 to Data_Array_2:

> type Data_Array_1 is array (Num_Types.Mod_4 range 1..8) of
> Num_Types.Mod_8;
> type Data_Array_2 is array (1..8) of Num_Types.Mod_8;

Uhm, converting is what instances of Unchecked_Conversion
do *not* do, in some (paradoxical) sense.
To convert X of type T to X of type D, write D(X).
An instance of Unchecked_Conversion will just leave
the bits unchanged. The programmer should then know the
meaning of the "converted" data.  An Ada array, however,
is a bit more than a sequence of storage cells,
when it includes bounds an other properties of the
corresponding array type.  Hence, arrays of different
type may be stored differenty.

In addition, if you choose "Integer range 1 .. 8" for
the index type (as per Bob's suggestion) the normal
representation of values of type integer may be
different from that of values of type Mod_4.
I imagine that even the instructions generated for indexing
may be different, e.g. indicating different register word
sizes for smaller or larger types. So Unchecked_Conversion
may yield unexpected results.

You can think of Ada arrays as tables where the keys
are values taken from the Index type. The keys will
not be strings, though, but either numbers, or letters,
or enumeration literals.  Maybe this helps to see why
one array is not like the other.


> type Data_Array_1 is array (Num_Types.Mod_4 range 1..8) of
> Num_Types.Mod_8;
> type Data_Array_2 is array (1..8) of Num_Types.Mod_8;
> 
> I would think that the way the array is indexed is (integer, mod_4, or
> whatever) wouldn't matter (it's the datatype of the elements of the
> array that matter).

No, that's not true for Ada arrays. Imagine
some book pages numbered i, ii, iii, iv, ...
while some others are numbered 1, 2, 3, 4, ...
Book printers will carefully observe this difference:
preface, TOC, etc. numbered i, ii, ... and then
the book's "proper text", numbered 1, 2, ...
This difference can be mapped to arrays index types,
reducing the risk of mixing the sequences of book
pages.


> So, will the convert function really in effect DO NOTHING but copy the
> data over?

It shouldn't have to copy at all, that's the point :-)



  parent reply	other threads:[~2009-05-18 20:53 UTC|newest]

Thread overview: 14+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2009-05-18 19:19 (Num_Types.Mod_4 range 1..8) ------->why not (1..8)? convergence82
2009-05-18 19:36 ` Georg Bauhaus
2009-05-18 19:58   ` convergence82
2009-05-18 20:50     ` Adam Beneschan
2009-05-18 20:53     ` Georg Bauhaus [this message]
2009-05-18 21:12       ` convergence82
2009-05-18 21:24       ` Adam Beneschan
2009-05-19  6:26         ` christoph.grein
2009-05-19  7:47           ` Petter
2009-05-19  8:04             ` christoph.grein
2009-05-19 13:13               ` Petter
2009-05-19 15:27                 ` Georg Bauhaus
2009-05-19 17:53           ` Adam Beneschan
2009-05-18 19:49 ` Robert A Duff
replies disabled

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