comp.lang.ada
 help / color / mirror / Atom feed
From: "Matthew Heaney" <mheaney@on2.com>
Subject: Re: Ada Basics
Date: Wed, 9 Oct 2002 14:37:35 -0400
Date: 2002-10-09T14:37:35-04:00	[thread overview]
Message-ID: <uq8trg8k5cr32c@corp.supernews.com> (raw)
In-Reply-To: d40d7104.0210090453.f6aed66@posting.google.com


"prashna" <vashwath@rediffmail.com> wrote in message
news:d40d7104.0210090453.f6aed66@posting.google.com...
> 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)?

As has been pointed out, the new form indicates derivation, which means the
primitive operations of the parent type are inherited by the derived type.

For type Integer, it's not obvious what's happening, but consider another
type:

package P is
   type T is range 0 .. 200;
   procedure Inc (O : in out T);
end;

Here, we have an integer type, that has (in addition to the pre-defined
operations for integer types) an increment operation, allowing you to do
this:

X : T := 0;
Increment (X);

Now let's create another type that derives from T:

with P;
package Q is
   type NT is new P.T;  --range is 0 .. 200
end;

Type NT inherits the operations of its parent type T, which means NT has an
increment operation:

Y : NT := 0;
Increment (Y);

In this example, T is the root of a family of types, and all types in this
family have an Inc operation.  This is useful if you want to pass any member
of this family as a generic formal type:

with P;
generic
   type NT is new P.T;
package GR is ...;

In this example, you can instantiate GR with either P.T or Q.NT:

with P;
package R is new GR (P.T);

with Q;
package R is new GR (Q.NT);

In the body of GR, you can apply the Inc operation to objects of formal type
NT.

For scalar types, you're allowed to constrain the range of the parent.  In
the example above, NT has the same range as its parent (0 .. 200), but we
could have made it smaller if that were desired:

with P;
package Q is
   type NT is new T range 1 .. 100;  --constrain range
end;

In general, a derivation is allowing you to state that there is a
relationship between the types.  If no such relationship is necessary, then
just declare the type without a derivation:

package Q is
   type T is range 0 .. 200;
end;







  parent reply	other threads:[~2002-10-09 18:37 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 [this message]
2002-10-09 18:58   ` David C. Hoos
2002-10-09 19:09     ` Matthew Heaney
2002-10-09 20:05 ` Robert A Duff
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