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-12 08:07:53 PST Path: supernews.google.com!sn-xit-02!supernews.com!news.gv.tsc.tdk.com!hub.org!hub.org!nntp1.njy.teleglobe.net!teleglobe.net!news-spur1.maxwell.syr.edu!news.maxwell.syr.edu!cpk-news-hub1.bbnplanet.com!cambridge1-snf1.gtei.net!news.gtei.net!inmet!not-for-mail From: Tucker Taft Newsgroups: comp.lang.ada Subject: Re: Types, subtypes and ranges Date: Mon, 12 Mar 2001 11:06:04 -0500 Organization: AverStar (formerly Intermetrics) Burlington, MA USA Message-ID: <3AACF3EC.6F85DE6C@averstar.com> References: <97pfmt$ll30@tech.port.ac.uk> <98idi9$66g$1@hobbes2.crc.com> NNTP-Posting-Host: nebula.burl.averstar.com Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit X-Trace: inmet2.burl.averstar.com 984413164 7855 141.199.8.77 (12 Mar 2001 16:06:04 GMT) X-Complaints-To: usenet@inmet2.burl.averstar.com NNTP-Posting-Date: 12 Mar 2001 16:06:04 GMT X-Mailer: Mozilla 4.75 [en] (X11; U; SunOS 5.7 sun4u) X-Accept-Language: en Xref: supernews.google.com comp.lang.ada:5641 Date: 2001-03-12T16:06:04+00:00 List-Id: "David C. Hoos, Sr." wrote: > > Because TTT'Image is inherited from its base type T. > > If you had declared TT and TTT as new T types, the constraint > error would have occurred, but then you wouldn't have been > able to add anders to banders without type conversion, because > the types would have been distinct. This isn't quite right. The 'Image and 'Value attributes deal with all values in the "base range" of the type, even if the prefix (e.g., the "S" in S'Image) is a subtype with a more constrained range. The "base range" of a scalar type is chosen by the implementation so that it is, if signed, symmetric around zero (+/- 1 value in negative direction), and generally corresponds to a range that is directly supported by the hardware. And of course, the base range must cover all the values specified in the original type definition. So don't expect 'Image or 'Value to do any useful constraint checking. Of course, when you assign the result of 'Value you will get constraint checking. The same goes for the 'Pos and 'Val attributes, FWIW. To answer your first question, declaring a subtype vs. declaring a new type -- all subtypes of the same type are freely interconvertible, whereas converting between distinct types requires an explicit conversion. Furthermore, all subtypes of the same type share the same set of operators/operations, and you can mix and match between subtypes for operands. In fact, "values" or "expressions" of an elementary type don't really have a subtype, they only have a "type." Subtypes are essentially subsets of types, whereas derived types represent a completely new universe of values, that happens to have the same number of values as the parent "universe," but the values are distinguishable because they have a different type. If you remember "Venn" diagrams from math class, subtypes are potentially overlapping circles on the Venn diagram representing a "universe" of the values of a type. On the other hand, a derived type gets its own separate Venn diagram, with its own possibly overlapping circles representing different subtypes. > > "Anders Wirzenius" wrote in message > news:V4%q6.26$OG2.2999@read2.inet.fi... > > > > Mark Lundquist wrote in message ... > > >Sorry for the late response (I lost access to news for a while...) > > > > > >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? > > > > > ... > > > subtype S is T; > > > > > >declares a new subtype S of the type of subtype T. This is also an > example > > >of a subtype declaration that does not include a constraint, so the > > >constraint of S is the same as the constraint of T. (This is how you > > >"rename" or create an alias for a type). What a subtype declaration > looks > > >like that adds a constraint depends on the type of the subtypes, e.g. for > > an > > >integer type you might have > > > > May I continue with two more questions: > > 1. > > given > > > > > > subtype S is T range 1 .. 10; > > > > what is the the difference between the above declaration and > > type S is new T range 1..10; > > > > 2. > > Why don't I get a constraint_error in the following code where I violate > the > > range of type TTT in the Put statement? > > > > with Ada.Text_IO; > > procedure Why_Not_Constraint_Error is > > type T is new Integer; > > subtype TT is T range -2..-1; > > subtype TTT is T range -1..-1; > > anders : TT := -2; > > bnders : TTT := -1; > > use Ada.Text_IO; > > begin > > Put ( TTT'Image ( anders+bnders ) ); > > end Why_Not_Constraint_Error; > > > > Anders > > another beginner > > > > > > -- -Tucker Taft stt@avercom.net http://www.averstar.com/~stt/ Chief Technology Officer, AverCom Corporation (A Titan Company) Burlington, MA USA (AverCom was formerly the Commercial Division of AverStar: http://www.averstar.com/services/ebusiness_applications.html)