comp.lang.ada
 help / color / mirror / Atom feed
From: "Matthew Heaney" <mheaney@on2.com>
Subject: Re: 'Base
Date: 8 Dec 2005 10:08:33 -0800
Date: 2005-12-08T10:08:33-08:00	[thread overview]
Message-ID: <1134065313.469475.267400@g47g2000cwa.googlegroups.com> (raw)
In-Reply-To: <1134055303.758950.308680@o13g2000cwo.googlegroups.com>


ada_student@yahoo.com wrote:
> What information does this attribute provide that cannot be specified
> using other Ada attributes?
>
> Why use an expression style to return a type? Is this because of
> the presence of parameterized types(i.e. generics) in Ada?


T'Base refers to the "base range" of the type, which defines the range
in which intermediate calculations are performed.

The standard states that the range of T'Base:

(1) includes the value 0
(2) is symmetric about zero, with possibly an extra negative value
(3) includes all of the values in the subtype T

So for example, if T is:

  type T is 1 .. 42;

then T'Base is

  type T'Base is -42 .. 42;

Note that built-in operators go through the base type, and T's "+" op
for example is implicitly declared as:

  function "+" (L, R : T'Base) return T'Base;

There are no constraint checks on T'Base, so for example:

declare
  O1 : T := T'(1) + T'(2);
  O2 : T'Base := T'(1) + T'(2)
begin

then in the first assignment to O1, there is a constraint check to
ensure that the result of 1 + 2 is in the range of T, but in the second
assignment to O2, there is no check.

T'Base is useful for generics, when you need to able to recover the
base range of the type, in order to declare a object with value 0; for
example, if this is an accumulator.

It's helpful to know something about the base range of the type, so
that you have a guarantee that you don't get any overflow during
intermediate calculations.  For example, given type T above then

procedure Op (O1, O2 : T) is
  Sum : T'Base := O1 + O2;
begin

This is a problem, since if the sum of O1 and O2 is large (that is,
greater than T'Base'Last), then you'll get overflow.  Knowing that
you're going to be adding two values together means you should declare
the type this way:

  T_Last : constant := 42;
  type T_Base is 0 .. 2 * T_Last;
  subtype T is T_Base range 1 .. T_Last;

That way you know that (sub)type T's range is 1 .. 42, but you also
have a guarantee that T'Base'Last >= 84, and hence the sum of two
values of type T cannot overflow.

Note that a declaration of the form:

  type T is range ...

actually declares a subtype, named T, of some anonymous base type.  We
can refer to the range of this base type as T'Base.

Note also that an enumeration type is its own base type, so given this
type:

  type ET is (A, B, C);

then the range of ET is the same as the range of ET'Base.  If you need
some extra literals in your "base" type, then you have to declare them
manually, not unlike what we did above:

  type ET_Base is (ET_Base_First, A, B, C, ET_Base_Last);
  subtype ET is ET_Base range A .. C;

Now you can say ET'Next (ET'Last) and you'll get a meaningful answer.
This is necessary when you do something like:

declare
  E : ET'Base := ET'First;
begin
  while E <= ET'Last then
     ... -- do something
     E := ET'Next (E);
  end loop;
end;


I might have some of the details wrong, but that's the general idea.

-Matt




  reply	other threads:[~2005-12-08 18:08 UTC|newest]

Thread overview: 33+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2005-12-08 15:21 'Base ada_student
2005-12-08 18:08 ` Matthew Heaney [this message]
2005-12-08 18:44   ` 'Base Martin Dowie
2005-12-08 18:49     ` 'Base Martin Dowie
2005-12-08 19:24     ` 'Base Matthew Heaney
2005-12-08 20:27       ` 'Base Martin Dowie
2005-12-08 19:51     ` 'Base Jeffrey R. Carter
2005-12-08 20:07       ` 'Base Matthew Heaney
2005-12-09  2:57         ` 'Base Randy Brukardt
2005-12-09  2:13   ` Avoiding constraint checks w/ 'Base Anonymous Coward
2005-12-09  3:11     ` Randy Brukardt
2005-12-09 13:11   ` 'Base krischik
2005-12-09 13:52     ` 'Base Matthew Heaney
2005-12-09 20:42       ` 'Base Randy Brukardt
2005-12-08 19:11 ` 'Base Martin Krischik
2005-12-09 20:42   ` 'Base ada_student
2005-12-09 21:39     ` 'Base Pascal Obry
2005-12-10  3:30     ` 'Base Matthew Heaney
2005-12-10 14:50       ` 'Base ada_student
2005-12-10  7:52     ` 'Base Martin Krischik
2005-12-10 12:55       ` 'Base Larry Kilgallen
2005-12-10 13:37         ` 'Base Björn Persson
2005-12-11 11:00           ` 'Base Martin Krischik
2005-12-10 15:01         ` 'Base Robert A Duff
2005-12-11 10:59         ` 'Base Martin Krischik
2005-12-12  9:14       ` 'Base Ole-Hjalmar Kristensen
2005-12-12 19:08         ` 'Base Martin Krischik
2005-12-13 19:24           ` 'Base tmoran
2005-12-13 21:00         ` 'Base Georg Bauhaus
2005-12-14 19:43         ` 'Base Per Sandberg
2005-12-15 20:08           ` 'Base Martin Krischik
2005-12-16 19:19             ` 'Base Jeffrey R. Carter
2005-12-17  7:52               ` 'Base Martin Krischik
replies disabled

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