comp.lang.ada
 help / color / mirror / Atom feed
From: Richard Riehle <laoXhai@ix.netcom.com>
Subject: Re: Types vs subtypes
Date: 2000/08/28
Date: 2000-08-28T23:20:24+00:00	[thread overview]
Message-ID: <39AAF3AB.3E5EE43E@ix.netcom.com> (raw)
In-Reply-To: 39a9d2b0@duster.adelaide.on.net






Alex Angas wrote:

> I've been given an Ada project to work on and I'm trying to work out what
> the difference between using a type or subtype is. Why is myarray an array
> of st, instead of just making it an array of t in the following declaration?
>
>  type enum is ( red, green, blue ) ;
>  type t is array( enum ) of natural ;
>  subtype st is t;
>  type myarray is array( enum ) of st;
>

This is something that comes up a lot in my Ada classes.   I try to explain it
this way:

A type has
                      1)  a name
                      2)  a set of operations
                      3)  a set of values
                      4)  a wall between objects of itself and objects of other
types with differing type names

The last item, " a wall" is important in distinguishing structural equivalence
from name equivalence in the
Ada type system.

A subtype has

                     1)  a name
                     2)  a parent type
                     3)  the set of operations of its parent type
                     4)  either the same set of values of its parent type or a
constrained set of values
                     5)  no wall between operations between itself and its
parent type
                     6)  no wall between itself and other subtypes derived from
its parent or from itself

A subtype is structurally equivalent to its parent type and its subtype
siblings, but may have a smaller
range of legal values.

This should not be confused with a derived type where the first four items for a
subtype will hold, but not
the last two (5 and 6).

         type T1 is (Red, Orange, Yellow, Green, Blue, Indigo, Violet);
         type T2 is (Red, Orange, Yellow, Green, Blue, Indigo, Violet);

These are both separate types.  The properties for a type hold.

        X : T1;
        Y : T2;
          ...
        X := Y;         -- illegal
        Y := X;         -- illegal
        X : T1(Y);    -- illegal; this type conversion not permitted in this
case

Now,

        type T3 is new T1;  -- could have constrained the range
        Z : T3;

         ...

        X := Z;          -- illegal;  no structural equivalence permitted for
types
        X := T1(Z);   -- legal;  T3 is derived from T1 and X and Z are in same
derivation

Further,

       subtype T4 is T1 range Green..Violet;
       subtype T5 is T1;
       M : T4;
       P  :  T5;
         ...
       X  := M;   -- legal;  structurally equivalent
       M := X;    -- also legal but potential for constraint error at execution
time.
       P  := X;    -- legal;  no potential for constraint error since subtype
range is same as parent

Summary.

Use subtypes whenever you feel OK about relaxing the typing model.   Use types
when you want to
preserve strong typing.   Ada has several features that, when used with care,
allow the programmer
to relax the type model.   It is easier to start with a language design in which
the default is strong
type and relax that model than to start with a language where the default is
weak typing and make
it stronger.

This is not a complete exposition and not very rigorous computer science, but I
hope it helps.

Richard Riehle









  reply	other threads:[~2000-08-28  0:00 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2000-08-28  2:47 Types vs subtypes Alex Angas
2000-08-28  0:00 ` Richard Riehle [this message]
2000-08-29  0:00   ` Marc A. Criley
2000-08-29  0:00     ` Jean-Pierre Rosen
2000-08-29  6:06   ` Preben Randhol
replies disabled

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