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=-0.3 required=5.0 tests=BAYES_00, REPLYTO_WITHOUT_TO_CC autolearn=no autolearn_force=no version=3.4.4 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 103376,9ab1bf4be2d855dd X-Google-Attributes: gid103376,public X-Google-ArrivalTime: 2001-03-18 14:31:04 PST Path: supernews.google.com!sn-xit-02!supernews.com!news.tele.dk!209.50.235.254!europa.netcrusader.net!63.208.208.143!feed2.onemain.com!feed1.onemain.com!newsfeed2.earthlink.net!newsfeed.earthlink.net!news.mindspring.net!not-for-mail From: Lao Xiao Hai Newsgroups: comp.lang.ada Subject: Re: Types, subtypes and ranges Date: Sun, 18 Mar 2001 14:28:17 -0800 Organization: AdaWorks Software Engineering Message-ID: <3AB53681.2959BAF7@ix.netcom.com> References: <97pfmt$ll30@tech.port.ac.uk> Reply-To: richard@adaworks.com NNTP-Posting-Host: 3f.35.b7.fd Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit X-Server-Date: 18 Mar 2001 22:27:17 GMT X-Mailer: Mozilla 4.7 [en] (Win98; I) X-Accept-Language: en Xref: supernews.google.com comp.lang.ada:5829 Date: 2001-03-18T22:27:17+00:00 List-Id: > > >WM 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