comp.lang.ada
 help / color / mirror / Atom feed
From: Lao Xiao Hai <laoxhai@ix.netcom.com>
Subject: Re: Types, subtypes and ranges
Date: Sun, 18 Mar 2001 14:28:17 -0800
Date: 2001-03-18T22:27:17+00:00	[thread overview]
Message-ID: <3AB53681.2959BAF7@ix.netcom.com> (raw)
In-Reply-To: V4%q6.26$OG2.2999@read2.inet.fi



>
> >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 a confusing issue for a newcomer to Ada.   One can give a strict
language-lawyer answer that a computer science purist will appreciate,
or one can give an answer that is useful to a working programmer.  So,
even though my answer may offend some who like to take the ALRM
seriously, I sometimes prefer an answer that can be used by someone
who just wants to grind out practical Ada code rather than argue about
how many subtypes can dance on the head of a pin.  Consequently, for
good or evil, here is another approach to answering this question.

A type has a:

          type name,
          set of values,
          set of associated operations,
          "wall" between objects of its type and objects of some other type

Under some circumstances, the "wall" may be breached through a type conversion.
The
rules for type conversion are appropriately strict.   One can also breach the
wall through
unsafe mechanisms such as type conversion, though these are often not
recommended.

A subtype

         has a subtype name,
         inherits the set of values of its parent type (or parent subtype)
         inherits the set of operations of its parent type (or parent subtype)
         has no wall between itself and its parent type or any subtype of its
parent.

Operations between subtypes with the same parent (ancestor) are legal without
the
need for type conversion.   Operations between a subtype and its ancestor are
also
legal without type conversion.  This means that subtypes need to be used with
care
because the compiler is not required to check for correct ranges.   It weakens
the
type system slightly, so many regard it as unsafe.

In addition, a subtype may have additional operations defined for it; and, in
some cases,
it may have a smaller set (range) of values.

The key difference between a type and subtype, for the practical programmer, is
the
absence of a wall between the subtype and its parent and sibling/cousin
subtypes.

I am fully aware of the more rigorous definition in the ALRM, but that
definition is
useful mainly for someone who is writing a compiler.   For an application
programmer,
the issue is the "wall" and what to do about it when designing with types and
subtypes.

Richard Riehle





  parent reply	other threads:[~2001-03-18 22:28 UTC|newest]

Thread overview: 14+ 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 ` Questions: Mark Lundquist
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 [this message]
2001-03-19 13:22       ` Marc A. Criley
2001-03-20 16:57         ` Lao Xiao Hai
  -- strict thread matches above, loose matches on Subject: below --
2001-03-14 13:50 Christoph Grein
replies disabled

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