comp.lang.ada
 help / color / mirror / Atom feed
* Ada Basics
@ 2002-10-09 12:53 prashna
  2002-10-09 15:27 ` Georg Bauhaus
                   ` (4 more replies)
  0 siblings, 5 replies; 9+ messages in thread
From: prashna @ 2002-10-09 12:53 UTC (permalink / raw)


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)?

Thanks in advance



^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: Ada Basics
  2002-10-09 12:53 Ada Basics prashna
@ 2002-10-09 15:27 ` Georg Bauhaus
  2002-10-09 15:50 ` Jerry Petrey
                   ` (3 subsequent siblings)
  4 siblings, 0 replies; 9+ messages in thread
From: Georg Bauhaus @ 2002-10-09 15:27 UTC (permalink / raw)


prashna <vashwath@rediffmail.com> wrote:
: 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)?

Have you been asked the same question? 
If so:
What is range 1..100 in the first example? Where does it appear?
What is range 1..100 in the second example? Where does it appear?
Otherwise, please forgive.

--  Georg



^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: Ada Basics
  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
                   ` (2 subsequent siblings)
  4 siblings, 1 reply; 9+ messages in thread
From: Jerry Petrey @ 2002-10-09 15:50 UTC (permalink / raw)




prashna wrote:

> 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)?
>
> Thanks in advance

The 'new' creates an explicit derived type which is a copy of the parent type
and inherits the primitive operations of the parent and any user defined
operations
of the parent before the derived type is defined.  It is a distinct type and
would
require a type conversion to be mixed with objects of the parent type, for
example.

In the example you cited, there would be little difference since all integer
types
are derived types - type one_to_hundred is range 1..100 creates a derived
type but the compiler chooses which predefined integer type to derive from (it
might for
example, choose short_integer).
With  one_to_hundred is new Integer range 1..100 , you are explicitly choosing

the parent type to be the predefined type Integer.

In Ada 95, derived types form the foundation of the polymorphic type
mechanism.
I would suggest you look at one of the good tutorials for more details on this

subject.  Look at http://www.adapower.org/ at the tutorials section.

Jerry
--
---------------------------------------------------------------------------------

-- Jerry Petrey
-- Senior Principal Systems Engineer - Navigation (GPS/INS), Guidance, &
Control
-- Raytheon Missile Systems          - Member Team Ada & Team Forth
-- NOTE: please remove <NOSPAM> in email address to reply
---------------------------------------------------------------------------------






^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: Ada Basics
  2002-10-09 15:50 ` Jerry Petrey
@ 2002-10-09 17:44   ` Wes Groleau
  0 siblings, 0 replies; 9+ messages in thread
From: Wes Groleau @ 2002-10-09 17:44 UTC (permalink / raw)




Jerry Petrey wrote:
> In the example you cited, there would be little difference since all integer
> types
> are derived types - type one_to_hundred is range 1..100 creates a derived
> type but the compiler chooses which predefined integer type to derive from (it
> might for
> example, choose short_integer).
> With  one_to_hundred is new Integer range 1..100 , you are explicitly choosing
> 
> the parent type to be the predefined type Integer.

The implementation (size, for example) is also inherited.
Which is why one might choose a particular parent.

But also, by making a derived type instead of a (plain) new type,
you may be sending a message to future readers that the parent
type specifically has operations that are important to the derived type.

-- 
Wes Groleau
http://freepages.rootsweb.com/~wgroleau



^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: Ada Basics
  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 18:37 ` Matthew Heaney
  2002-10-09 18:58   ` David C. Hoos
  2002-10-09 20:05 ` Robert A Duff
  2002-10-10 10:47 ` prashna
  4 siblings, 1 reply; 9+ messages in thread
From: Matthew Heaney @ 2002-10-09 18:37 UTC (permalink / raw)



"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;







^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: Ada Basics
  2002-10-09 18:37 ` Matthew Heaney
@ 2002-10-09 18:58   ` David C. Hoos
  2002-10-09 19:09     ` Matthew Heaney
  0 siblings, 1 reply; 9+ messages in thread
From: David C. Hoos @ 2002-10-09 18:58 UTC (permalink / raw)


Where is the procedure Increment declared?

----- Original Message -----
From: "Matthew Heaney" <mheaney@on2.com>
Newsgroups: comp.lang.ada
To: <comp.lang.ada@ada.eu.org>
Sent: Wednesday, October 09, 2002 1:37 PM
Subject: Re: Ada Basics


>
> "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;
>
>
>
>
> _______________________________________________
> comp.lang.ada mailing list
> comp.lang.ada@ada.eu.org
> http://ada.eu.org/mailman/listinfo/comp.lang.ada
>




^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: Ada Basics
  2002-10-09 18:58   ` David C. Hoos
@ 2002-10-09 19:09     ` Matthew Heaney
  0 siblings, 0 replies; 9+ messages in thread
From: Matthew Heaney @ 2002-10-09 19:09 UTC (permalink / raw)



"David C. Hoos" <david.c.hoos.sr@ada95.com> wrote in message
news:mailman.1034189822.26075.comp.lang.ada@ada.eu.org...
> Where is the procedure Increment declared?

I meant Inc.

Inc is declared in P.

Q.NT inherits P.Inc.

I should have pointed out that Q.NT, in addition to constraining its
parent's range, can also override the operations it inherits.  For example:

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

with P;
package Q is
   type NT is new P.T;
   procedure Inc (O : in out NT); --override P.Inc
end;







^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: Ada Basics
  2002-10-09 12:53 Ada Basics prashna
                   ` (2 preceding siblings ...)
  2002-10-09 18:37 ` Matthew Heaney
@ 2002-10-09 20:05 ` Robert A Duff
  2002-10-10 10:47 ` prashna
  4 siblings, 0 replies; 9+ messages in thread
From: Robert A Duff @ 2002-10-09 20:05 UTC (permalink / raw)


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



^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: Ada Basics
  2002-10-09 12:53 Ada Basics prashna
                   ` (3 preceding siblings ...)
  2002-10-09 20:05 ` Robert A Duff
@ 2002-10-10 10:47 ` prashna
  4 siblings, 0 replies; 9+ messages in thread
From: prashna @ 2002-10-10 10:47 UTC (permalink / raw)


Thanks to all, thank u very much.
vashwath@rediffmail.com (prashna) 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)?
> 
> Thanks in advance



^ permalink raw reply	[flat|nested] 9+ messages in thread

end of thread, other threads:[~2002-10-10 10:47 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
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
2002-10-10 10:47 ` prashna

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