comp.lang.ada
 help / color / mirror / Atom feed
From: Tucker Taft <stt@averstar.com>
Subject: Re: to type or subtype ? (novice question, three parts)
Date: 2000/02/15
Date: 2000-02-15T16:57:35+00:00	[thread overview]
Message-ID: <38A9857F.44A4172B@averstar.com> (raw)
In-Reply-To: 38A94303.125C337F@interact.net.au

G wrote:
> 
> Hi all, and thanks so much for all your help. :-)
> I hope this is all relevant to the newsgroup topic.
> There are not as many diverse sources about Ada as there are
> other languages, so it is sometimes difficult to get helpful
> information.
> I try to get it straight from the horse's mouth, as it were...
> 
> There are three questions in this message.
> One is about types and subtypes.
> Two is about signed modular integers.
> Three is about a very little bit of MIL-STD-1553B.
> 
> ------------------------------------------
> ------------------------------------------
> 1:
> Ok, something I don't understand - when should you
> make a new type, say :
> 
> type widget is new integer;
> 
>   or, say :
> 
> subtype widget is integer;
> 
> - when they are both integers.  That is -
> what difference does it make with what you
> can do with/to the type ?  

You should use distinct types when you don't want to mistakenly
combine things which are fundamentally different, even though they
share the same underlying representation.  In Ada, there is
no implicit conversion between user-declared types, so if you
have two variables that you want to assign back and forth, or combine
using some arithmetic or logic operator, then they should generally
be the same type.  If they represent fundamentally different things,
like age and weight, then you definitely want different types, even
though both can use "plain old integers" as the underlying representation.

In your example, you have "byte" and "bitmask."  They sound like different
things to me, presuming a byte is an 8-bit numeric value, whereas a bitmask
is essentially a vector of 8 booleans.  Of course, you could declare
it as a packed array of 8 booleans, though that might result in
pass-by-reference on some compilers, which would perhaps be undesirable.

Even "bitmask" sounds a bit generic.  Perhaps what you really want is
Blodget_Flags or Wombat_Attributes or some such thing.  It is hard to
know without knowing your application better.

> ...
> 
> ------------------------------------------------------------
> ------------------------------------------------------------
> 
> 2:
> 
> Modular integers and the interfaces.unsigned_32 types -
> it appears as though (after some compiler experimentation)
> that modular integers are always going to be signed and should
> have to be the only type I can use for signed bytes, words, etc.
> 
> no ? yes ? maybe ?

Now you have confused me.  Modular integers are *unsigned* in Ada.
That is, they have values in the range 0..modulus-1.
Signed integers are declared via "type Sgn is range -100..100;" and
include negative values in their "base" range (even if not in their
first subtype range).

> 
> (I am trying to use Ada to experiment & learn with concepts I get from
> all over the place
> in technical manuals, tutorials, etc.  I have found
> interfaces.unsigned_xx to allow
> values in the range 2*3 .. 2*6.  ) I use GNAT 3.11.
> ------------------------------------------------
> ------------------------------------------------
> 3.
> In an implementation of MIL-STD-1553B I have read,
> there is a declaration:
> 
> type Bit_Numbers is range 0 .. 15;
> 
> - I am wondering why anyone would want to work with
> individual bits (that is what I interpret that as for) in this
> way, because you can alter their values with logical bit_masks.
> I was reading that the 80x86 works more efficiently with bytes
> than with the smaller bits.  Would you want to work with individual
> bits in some situations because some embedded/special-purpose
> computers may have different architectures which make
> it relevant to manipulate individual bits in this manner ?

There are at least two reasons to specify a range.  One is to
implicity request a type of a particular size (e.g. 4 bits in this case).
The other is because the range represents the meaningful range of
values, and anything outside of that range would be an indication
of a bug.  Generally in Ada you would use the range to implicit
specify the type only when interfacing with some external device or
format.  In all other cases, you would want to specify a range that
corresponds to the logical/semantic properties of the type, and you
only care that the underlying physical representation be sufficiently
large to accommodate the full meaningful range.


> 
> -------
> -------
> - I am struggling to understand, sorry if this is irrelevant.

Not irrelevant at all.  Most programming languages are totally wedded
to the reprentation-oriented view of numeric types.  Ada is very rare
in that it encourages the programmer to think of numeric types at a higher
level, and gives the programmer the ability to attach application-specific
semantics to particular numeric types.  About the closest thing to this
in other languages is enumeration types, which we all know are represented
internally as integers, but for which the values have some non-numeric
significance.  One fairly good model is the notion of "units" from
physics, where a number rarely has any meaning on its own.  You need
to qualify it with the particular units (e.g. feet, meters, Hz, light-years,
etc.) to know what the number "means."  In Ada, the model is the same.
A numeric value has no real meaning until you know what "domain" it
comes from, or what units it has.

> --
> GM Wallace.
> 
> BDF
> --

-- 
-Tucker Taft   stt@averstar.com   http://www.averstar.com/~stt/
Technical Director, Distributed IT Solutions  (www.averstar.com/tools)
AverStar (formerly Intermetrics, Inc.)   Burlington, MA  USA




      reply	other threads:[~2000-02-15  0:00 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2000-02-15  0:00 to type or subtype ? (novice question, three parts) G
2000-02-15  0:00 ` Tucker Taft [this message]
replies disabled

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