comp.lang.ada
 help / color / mirror / Atom feed
From: Wilhelm Spickermann <zhopi.20.unbenutzbar@spamgourmet.com>
Subject: Re: types and subtypes
Date: Mon, 13 Mar 2006 21:22:16 +0100
Date: 2006-03-13T21:22:16+01:00	[thread overview]
Message-ID: <dv4k9o$m3b$1@ulysses.news.tiscali.de> (raw)
In-Reply-To: 1142273325.634632.41020@j52g2000cwj.googlegroups.com

ada_student@yahoo.com wrote:

> A type also defines the set of values that an object can have.

There is a difference between the set of values for a variable
and the set of values for an expression. That's the key to
understanding here.

> If an Ada subtype adds a constraint to an Ada type, then the
> subtype is a type that is a derivative(derivative as in the Ada
> sense) of the Ada type and is different from that type.

A subtype is not a type; no constraint is added to the type; the
type remains unchanged; no new type is created. 

As far as simple types like numbers are concerned: A subtype is
just a name used to create variables which are restricted to a
subset of the possible values of the type. Values/Expressions
are not of a subtype -- they just belong to a type. The subtype
restrictions only apply, when an assignment to a variable of a
subtype is done.
If A is a variable of your subtype Count_To_Ten, then you may
write " if A*3-8 > 15 then ...". No subtype restrictions are
applied to A*3 or A*3-8 or 15. But the information about the
restricted set of values for A is used in the optimization --
Integer will not overflow during this computation :-) .
Now if you have a variable B of your subtype Positive and you
write B := A*3-8, then all computations can be done without
problems as all values involved are of type Integer. Only when
assigning the result to B, you may get a Constraint_Error.

Hope this helps in understanding
Wilhelm




  parent reply	other threads:[~2006-03-13 20:22 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2006-03-13 12:07 types and subtypes ada_student
2006-03-13 13:20 ` Jean-Pierre Rosen
2006-03-13 18:08   ` ada_student
2006-03-13 18:17     ` Ed Falis
2006-03-13 19:14     ` Larry Kilgallen
2006-03-13 19:42     ` Martin Krischik
2006-03-13 20:22     ` Wilhelm Spickermann [this message]
2006-03-14  8:47     ` Dmitry A. Kazakov
2006-03-14 14:39     ` Jean-Pierre Rosen
2006-03-17  1:24     ` Peter C. Chapin
replies disabled

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