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.9 required=5.0 tests=BAYES_00 autolearn=ham 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-19 05:26:07 PST Path: supernews.google.com!sn-xit-03!supernews.com!cyclone2.usenetserver.com!news-out.usenetserver.com!newsfeed2.earthlink.net!newsfeed.earthlink.net!newsmaster1.prod.itd.earthlink.net!newsread1.prod.itd.earthlink.net.POSTED!not-for-mail Message-ID: <3AB5FA4A.1779F976@earthlink.net> From: "Marc A. Criley" Organization: Quadrus Corporation X-Mailer: Mozilla 4.73 [en] (X11; U; Linux 2.2.14-5.0 i686) X-Accept-Language: en MIME-Version: 1.0 Newsgroups: comp.lang.ada Subject: Re: Types, subtypes and ranges References: <97pfmt$ll30@tech.port.ac.uk> <3AB53681.2959BAF7@ix.netcom.com> Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit Date: Mon, 19 Mar 2001 13:22:02 GMT NNTP-Posting-Host: 158.252.123.147 X-Complaints-To: abuse@earthlink.net X-Trace: newsread1.prod.itd.earthlink.net 985008122 158.252.123.147 (Mon, 19 Mar 2001 05:22:02 PST) NNTP-Posting-Date: Mon, 19 Mar 2001 05:22:02 PST Xref: supernews.google.com comp.lang.ada:5839 Date: 2001-03-19T13:22:02+00:00 List-Id: Lao Xiao Hai wrote: > > > > > >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? > > > A subtype [...] > 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. Hmmm, I've never really considered subtypes a "weakening" of the type slightly, though obviously they could be ill-used to that end. My use of them has been in fact to tighten up the typing. One useful use of subtypes is with indexing. For example: type Item_Count is range 0 .. Max_Items; subtype Indexes is Item_Count range 1 .. Max_Items; type Item_List is array(Indexes) of Item_Type; Items : Item_List; Number_Of_Items : Item_Count := 0; So now I can increment Number_Of_Items and use that as the index into the Item_List array while ensuring that only valid indexes will be available. Another useful area where subtypes can increase type safety is when you're working with discriminated records. type Operation is (Load, Store, Clear); type Command(Op : Operation) is record ... end record; Now if you want to require that only data of a specific kind is allowed you can subtype the record: subtype Store_Command is Command(Store); In this case any variables of type Store_Command must conform to that value of the discriminant. An actual instance of this latter approach is used extensively in a particular US Navy weapon control system. Messages that came in over a socket had their header read to determine what type of message was being transmitted. Then within a declare/begin/end block the Message variant record type was subtyped with that type of message, and then the message was read off the socket directly into the message. Works slick, shoots missiles! :-) Marc A. Criley Senior Staff Engineer Quadrus Corporation www.quadruscorp.com