comp.lang.ada
 help / color / mirror / Atom feed
From: Robert A Duff <bobduff@shell01.TheWorld.com>
Subject: Re: Ada Basics
Date: Wed, 9 Oct 2002 20:05:21 GMT
Date: 2002-10-09T20:05:21+00:00	[thread overview]
Message-ID: <wcc1y6z4bum.fsf@shell01.TheWorld.com> (raw)
In-Reply-To: d40d7104.0210090453.f6aed66@posting.google.com

vashwath@rediffmail.com (prashna) writes:

> Hi all,
> What is the difference between declaring a type by using new (ex type
> one_to_hundred is new Integer range 1..100) and declaring a type
> without using new (type one_to_hundred is range 1..100)?

There is *almost* no difference.  I would usually choose the latter,
if the new type has nothing to do with Standard.Integer.

The only semantic difference is in the base range.  In the former case,
One_To_Hundred'Base'Last = Integer'Last, whereas in the latter case,
One_To_Hundred'Base'Last >= 100.  The base range matters for
intermediate expression results, so:

    X: One_To_Hundred := 100;
    Y: One_To_Hundred := 100;
    Z: One_To_Hundred := 100;

    W: One_To_Hundred := (X + Y + Z) / 3;

we have an intermediate result equal to 300.  In the latter case, the
implementation *could* raise Constraint_Error (if it chose the base
range to be, say, -128..127).  In the former case, C_E cannot be raised,
because we know Standard.Integer'Last is at least 32,767, and most
likely 2 billion, or more.

You could also guarantee that W = 100 (rather than raising C_E) like
this:

    type Intermediate is range -300..300;
    subtype One_To_Hundred is Intermediate range 1..100;

But if you don't have intermediate expression results that lie outside
the bounds of the type, then both of the above are equivalent, and if
all you care about is the 1..100 range, then "type T is range 1..100" is
probably the best style.

- Bob



  parent reply	other threads:[~2002-10-09 20:05 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2002-10-09 12:53 Ada Basics prashna
2002-10-09 15:27 ` Georg Bauhaus
2002-10-09 15:50 ` Jerry Petrey
2002-10-09 17:44   ` Wes Groleau
2002-10-09 18:37 ` Matthew Heaney
2002-10-09 18:58   ` David C. Hoos
2002-10-09 19:09     ` Matthew Heaney
2002-10-09 20:05 ` Robert A Duff [this message]
2002-10-10 10:47 ` prashna
replies disabled

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