* Re: types and subtypes
2006-03-13 18:08 ` ada_student
@ 2006-03-13 18:17 ` Ed Falis
2006-03-13 19:14 ` Larry Kilgallen
` (5 subsequent siblings)
6 siblings, 0 replies; 10+ messages in thread
From: Ed Falis @ 2006-03-13 18:17 UTC (permalink / raw)
On Mon, 13 Mar 2006 13:08:45 -0500, <ada_student@yahoo.com> wrote:
> Why was Ada's subtyping defined to exclude derivation?
Derived types in Ada, whether tagged or not, are the same as the
conventional usage of the term "subtype" in other OO languages. Subtypes
in Ada are about adding constraints on the range of acceptable values,
rather than emphasizing type extension and overriding of subprograms,
which is the role of a derived type.
Ada's use of the term "subtype" preceded the development of most OO
languages (Simula 67 being a noteworthy exception, and probably Smalltalk).
- Ed
^ permalink raw reply [flat|nested] 10+ messages in thread
* Re: types and subtypes
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
` (4 subsequent siblings)
6 siblings, 0 replies; 10+ messages in thread
From: Larry Kilgallen @ 2006-03-13 19:14 UTC (permalink / raw)
In article <1142273325.634632.41020@j52g2000cwj.googlegroups.com>, ada_student@yahoo.com writes:
> Why doesnt Ada subtyping also denote derivation as in the
> sense C++ base classes and derived classes do?
Perhaps because:
1. They are different languages.
2. C++ did not exist at the same time.
I haven't followed the whole discussion, but an Ada type can be
derived from another Ada type. Subtypes are different.
^ permalink raw reply [flat|nested] 10+ messages in thread
* Re: types and subtypes
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
` (3 subsequent siblings)
6 siblings, 0 replies; 10+ messages in thread
From: Martin Krischik @ 2006-03-13 19:42 UTC (permalink / raw)
ada_student@yahoo.com wrote:
> Why doesnt Ada subtyping also denote derivation as in the
> sense C++ base classes and derived classes do?
Since you know C++: subtype is more like typedef. i.E.:
class C {};
typedef C A;
typedef C B;
F1(A x);
F2(B x);
As you are experienced in C++ you will know that this won't work as both A
and B are typedefs of C and hence the same type.
Martin
--
mailto://krischik@users.sourceforge.net
Ada programming at: http://ada.krischik.com
^ permalink raw reply [flat|nested] 10+ messages in thread
* Re: types and subtypes
2006-03-13 18:08 ` ada_student
` (2 preceding siblings ...)
2006-03-13 19:42 ` Martin Krischik
@ 2006-03-13 20:22 ` Wilhelm Spickermann
2006-03-14 8:47 ` Dmitry A. Kazakov
` (2 subsequent siblings)
6 siblings, 0 replies; 10+ messages in thread
From: Wilhelm Spickermann @ 2006-03-13 20:22 UTC (permalink / raw)
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
^ permalink raw reply [flat|nested] 10+ messages in thread
* Re: types and subtypes
2006-03-13 18:08 ` ada_student
` (3 preceding siblings ...)
2006-03-13 20:22 ` Wilhelm Spickermann
@ 2006-03-14 8:47 ` Dmitry A. Kazakov
2006-03-14 14:39 ` Jean-Pierre Rosen
2006-03-17 1:24 ` Peter C. Chapin
6 siblings, 0 replies; 10+ messages in thread
From: Dmitry A. Kazakov @ 2006-03-14 8:47 UTC (permalink / raw)
On 13 Mar 2006 10:08:45 -0800, ada_student@yahoo.com wrote:
> A type also defines the set of values that an object can have.
> 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.
Yes it is, but that does not prevent the new type to have the same
representation and be substitutable for the base one. You can consider a
subtype as a new type for which the compiler routinely applies identity
conversions. Downward conversions additionally check the constraint. So it
is consistent with strong typing, and the notion of subtype is nothing but
just a type relation. There is nothing except types.
> [I know you can say "type T2 is new T1 ..." in Ada to denote
> derivation]
That was IMO a bad design decision. It should be "subtype T2 is T1 with".
But that's my personal opinion.
> Why doesnt Ada subtyping also denote derivation as in the
> sense C++ base classes and derived classes do?
>
> Why was Ada's subtyping defined to exclude derivation?
Probably because it seemed conceptually simpler. Ada 83 had subtype = same
representation + constraint. Ada 95 added derived tagged extension = same
representation + extension part. If we drop "same representation", which is
clearly an implementation detail, we will see that there is no difference.
--
Regards,
Dmitry A. Kazakov
http://www.dmitry-kazakov.de
^ permalink raw reply [flat|nested] 10+ messages in thread
* Re: types and subtypes
2006-03-13 18:08 ` ada_student
` (4 preceding siblings ...)
2006-03-14 8:47 ` Dmitry A. Kazakov
@ 2006-03-14 14:39 ` Jean-Pierre Rosen
2006-03-17 1:24 ` Peter C. Chapin
6 siblings, 0 replies; 10+ messages in thread
From: Jean-Pierre Rosen @ 2006-03-14 14:39 UTC (permalink / raw)
ada_student@yahoo.com a �crit :
>> Because they are of the same *type*. A subtype does not declare a
>> different type, only a restriction (a *constraint*) on the allowed
>> values. Since the types are the same, the declaration is OK.
>>
>
> Then Ada's definition of a type is different from other languages.
No, Ada's definition of a *sub*type is different.
> A type also defines the set of values that an object can have.
That's correct.
In other languages, a subtype is a sub-type, i.e. a type "sub" (under)
another type.
In Ada, a type is a set of value; a subtype is a subset of that set.
Since a subset is included in the set, a subtype is included in (and of
same nature as) the type.
--
---------------------------------------------------------
J-P. Rosen (rosen@adalog.fr)
Visit Adalog's web site at http://www.adalog.fr
^ permalink raw reply [flat|nested] 10+ messages in thread
* Re: types and subtypes
2006-03-13 18:08 ` ada_student
` (5 preceding siblings ...)
2006-03-14 14:39 ` Jean-Pierre Rosen
@ 2006-03-17 1:24 ` Peter C. Chapin
6 siblings, 0 replies; 10+ messages in thread
From: Peter C. Chapin @ 2006-03-17 1:24 UTC (permalink / raw)
ada_student@yahoo.com wrote in news:1142273325.634632.41020
@j52g2000cwj.googlegroups.com:
> Why doesnt Ada subtyping also denote derivation as in the
> sense C++ base classes and derived classes do?
>
> Why was Ada's subtyping defined to exclude derivation?
Consider this...
Suppose you had a C++ base class Vehicle from which you derived a class
Car.
class Vehicle { };
class Car : public Vehicle { };
In OO terms, Car is a "subtype" of Vehicle. But this is consistent with
Ada's Positive being a subtype of Integer. The set of Car values is a
subset of the set of Vehicle values just as the set of Positive values
is a subset of the set of Integer values. To say a Vehicle is a Car is
to restrict it... no longer are you talking about just any Vehicle. It
is the same with Ada subtypes as far as I can see.
Of course it is likely that class Car has more members than class
Vehicle and thus has an extended implementation. However it defines a
more restricted class of objects.
Peter
^ permalink raw reply [flat|nested] 10+ messages in thread