comp.lang.ada
 help / color / mirror / Atom feed
From: "Randy Brukardt" <randy@rrsoftware.com>
Subject: Re: A question re meaning/use of the "for ... use ..."
Date: Fri, 10 Dec 2004 13:42:57 -0600
Date: 2004-12-10T13:42:57-06:00	[thread overview]
Message-ID: <vtKdnSCJUc9uZiTcRVn-ow@megapath.net> (raw)
In-Reply-To: ln1xdygaf2.fsf@nuthaus.mib.org

"Keith Thompson" <kst-u@mib.org> wrote in message
news:ln1xdygaf2.fsf@nuthaus.mib.org...
> Here's an example where it doesn't work:
...

If you want an unsigned type, then use a modular type:
    type Enum_Rep is mod 2**Enum'Size;
    for Enum_Rep'Size use Enum'Size;

I know that you're now going to say that that doesn't work for a signed
representation, but that's a red herring. You have the know whether the
representation is signed or unsigned, even for your hypothetical From_Rep
attribute, because you have to decide on what sort of type to put it into.
Otherwise, you're at risk of raising Constraint_Error. (On a typical
implementation, there are modular values that don't fit into any signed
type, and of course negative values don't fit into any modular type.) Even a
non-static universal expression isn't safe: it can raise Constraint_Error
for modular values that aren't in any signed type. One example:

    generic
         type Something is (<>);
    package P is
         ...
    end P;
    package body P
         ...
         if Something'Pos(Something'Last) > 10 then -- (!)
         ...
    end P;

The expression marked (!) can (and will on many implementations) raise
Constraint_Error, because Something'Pos(Something'Last) is outside of the
range of Root_Integer.

So you really have to know which you are dealing with. For enumeration
representations, assuming modular is probably the best option, as you are
usually dealing with bit patterns.

But this whole issue comes from people trying to use enumeration
representations for purposes for which they are not intended. They're only
intended to be used for *external* representations, which means that the
program itself has no reason to be concerned with them. The program should
use the internal representation.

If you really need a discontiguous counting type, that's something that Ada
doesn't provide and you should program it yourself. That's a very rare need;
you really need to think carefully if you really need such a type.

The issue is very similar to Bit_Order, which only has to do with the way
bits are numbered in the program; it has nothing to do with the bit or byte
order in the external world. But people keep hoping that it would be,
because that would make someone else solve their hard problems.

                     Randy.






  reply	other threads:[~2004-12-10 19:42 UTC|newest]

Thread overview: 23+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2004-12-05 15:27 A question re meaning/use of the "for ... use ..." Erik J Pessers
2004-12-05 15:47 ` Martin Krischik
2004-12-05 15:59 ` Stephen Leake
2004-12-05 16:52   ` Jeffrey Carter
2004-12-06 19:59     ` Randy Brukardt
2004-12-07  1:36       ` Jeffrey Carter
2004-12-07  2:40         ` David C. Hoos, Sr.
2004-12-07 20:59         ` Randy Brukardt
2004-12-08  1:41           ` Jeffrey Carter
2004-12-08  8:40           ` Martin Dowie
2004-12-08 16:23             ` Georg Bauhaus
2004-12-08  3:18       ` Keith Thompson
2004-12-08 13:48         ` David C. Hoos
2004-12-08 19:50         ` Randy Brukardt
2004-12-08 23:00           ` Keith Thompson
2004-12-09 23:06             ` Randy Brukardt
2004-12-10  2:26               ` Keith Thompson
2004-12-10 19:42                 ` Randy Brukardt [this message]
2004-12-10 21:18                   ` Keith Thompson
2004-12-11  0:53                     ` Keith Thompson
2004-12-10  3:13             ` David C. Hoos
2004-12-10  9:23               ` Keith Thompson
2004-12-10 13:24                 ` David C. Hoos
replies disabled

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