comp.lang.ada
 help / color / mirror / Atom feed
From: "Mark Lundquist" <mark@rational.com>
Subject: Re: Questions:
Date: Wed, 07 Mar 2001 23:05:28 GMT
Date: 2001-03-07T23:05:28+00:00	[thread overview]
Message-ID: <Y8zp6.524529$U46.15710729@news1.sttls1.wa.home.com> (raw)
In-Reply-To: 97pfmt$ll30@tech.port.ac.uk

Sorry for the late response (I lost access to news for a while...)

WM <wwminirl@hotmail.com> wrote in message
news:97pfmt$ll30@tech.port.ac.uk...
> Hi, I am an ada beginner. Can anyone kindly tell me what's the difference
> between TYPE and SUBTYPE?

This is one of the cool things about Ada, IMHO.  It's also very fundamental
and one of the keys to "thinking in Ada".  It's not well understood by those
who learn the language in a "paint-by-numbers" way, but it's really not that
complicated, and it's well worth understanding.

First of all, what it *doesn't* mean... "subtype" isn't a relationship, it's
a kind of entity (so you don't say, "type Foo is a subtype of type Bar").
Nor is a subtype a "kind of type".  Also, subtypes have nothing to do with
inheritance.  The mechanism for inheritance is type *derivation* (in OO
literature, the terms "subtype" and "derivation" are both used to denote
inheritance; Ada uses the term "derivation" to denote inheritance, and it
uses the term "subtype" to mean a very different thing...)

Type and subtype are distinctly different entities.  But every subtype is
associated with some type, which is called "the type of the subtype".

Every object has (is "of") a subtype! (and thus also a type).

But a *value* does not have a subtype!  It has only a type!

A type is a set of values, combined with a set of operations.  A subtype
combines a type with a *constraint*, which describes a subset of the type's
values that objects of the subtype are allowed to have.

Got that?  Type = values + operations.  Subtype = type + constraint.

A "null constraint" is a constraint that doesn't restrict the set of values.

Now then... every type declaration actually declares *two* things: a type
and a subtype!  This subtype is called the "first subtype" of the type.
Technically, types are anonymous in Ada; what is usually thought of as the
"type name" is really the name of the first subtype.

A subtype declaration declares only a subtype.  The type of the new subtype
is the same as the type of the subtype named in the declaration, e.g. given

    subtype S is T;

declares a new subtype S of the type of subtype T.  This is also an example
of a subtype declaration that does not include a constraint, so the
constraint of S is the same as the constraint of T.  (This is how you
"rename" or create an alias for a type).  What a subtype declaration looks
like that adds a constraint depends on the type of the subtypes, e.g. for an
integer type you might have

    subtype S is T range 1 .. 10;


Hope this helps,

Mark Lundquist
Rational Software






  parent reply	other threads:[~2001-03-07 23:05 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2001-03-03  1:00 Questions: WM
2001-03-03  3:30 ` Questions: Robert Love
2001-03-03 11:09 ` Questions: David C. Hoos, Sr.
2001-03-07 23:05 ` Mark Lundquist [this message]
2001-03-08  1:14   ` Questions: Robert A Duff
2001-03-12  7:41   ` Types, subtypes and ranges Anders Wirzenius
2001-03-12 11:57     ` David C. Hoos, Sr.
2001-03-12 16:06       ` Tucker Taft
2001-03-13  6:40       ` Anders Wirzenius
2001-03-12 16:57     ` Scott Ingram
2001-03-18 22:28     ` Lao Xiao Hai
2001-03-19 13:22       ` Marc A. Criley
2001-03-20 16:57         ` Lao Xiao Hai
replies disabled

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