From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: X-Spam-Status: No, score=-1.3 required=5.0 tests=BAYES_00,INVALID_MSGID autolearn=no autolearn_force=no version=3.4.4 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 103376,49c69fcb0de2bb74 X-Google-Attributes: gid103376,public From: Richard Riehle Subject: Re: Types vs subtypes Date: 2000/08/28 Message-ID: <39AAF3AB.3E5EE43E@ix.netcom.com>#1/1 X-Deja-AN: 663678210 Content-Transfer-Encoding: 7bit References: <39a9d2b0@duster.adelaide.on.net> X-Accept-Language: en X-Server-Date: 28 Aug 2000 23:20:24 GMT Content-Type: text/plain; charset=us-ascii Organization: MindSpring Enterprises Mime-Version: 1.0 Newsgroups: comp.lang.ada Date: 2000-08-28T23:20:24+00:00 List-Id: 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