comp.lang.ada
 help / color / mirror / Atom feed
From: "Norman H. Cohen" <ncohen@watson.ibm.com>
Subject: Re: strong typing
Date: 1996/10/10
Date: 1996-10-10T00:00:00+00:00	[thread overview]
Message-ID: <325D0247.FEC@watson.ibm.com> (raw)
In-Reply-To: 53e6n5$eq2@rc1.vub.ac.be


AGBOH CHARLES wrote:

> Ada's strong typing mechanism doest not allow identifiers of different
> types to assigned to each other.  Why are subtypes and parent type
> permitted. Why can derived types and parent types be assigned to each other.
> Apparently a derived type and sybtypes are new types so assignment should not
> be possible.

A subtype is NOT a distinct type, but simply a subset of its type. Given
the declaration

   X: Character := 'N';

the current value of X belongs to type Character, to the subtype
Character range 'A' .. 'Z', to the subtype Character range 'G' .. 'R',
etc.  Indeed, Character itself is a subtype, consisting of every value
of the type.  A declaration such as

   subtype ST is Character range 'A' .. 'F';

simply establishes ST as a shorthand for Character range 'A' .. 'F'. 
(In some places, such as parameter specifications, the syntax of Ada
only allows subtype names, so this shorthand is sometimes vital.)

In contrast, a derived type IS a new, distinct type, and assignment
between a derived type and it parent type is NOT allowed.  However, a
value of a derived type can be CONVERTED to a value of its parent type,
and the converted value can be assigned.  (Conversion in the other
direction is also possible when the types are not tagged.)

Shameless commercial :-) :  For a fuller discussion, see Chapter 4 of
Ada as a Second Language.  The chapter is devoted to these issues.

-- 
Norman H. Cohen
mailto:ncohen@watson.ibm.com
http://www.research.ibm.com/people/n/ncohen




  reply	other threads:[~1996-10-10  0:00 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
1996-10-08  0:00 strong typing AGBOH CHARLES
1996-10-10  0:00 ` Norman H. Cohen [this message]
  -- strict thread matches above, loose matches on Subject: below --
1990-01-05  1:28 Mark Gerhardt
replies disabled

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