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,88093378be1184d4 X-Google-Attributes: gid103376,public X-Google-ArrivalTime: 2001-11-07 03:00:53 PST Path: archiver1.google.com!news1.google.com!newsfeed.stanford.edu!news.tele.dk!small.news.tele.dk!195.54.122.107!newsfeed1.bredband.com!bredband!uio.no!ntnu.no!not-for-mail From: Preben Randhol Newsgroups: comp.lang.ada Subject: Re: A question and a request Date: Wed, 7 Nov 2001 11:00:49 +0000 (UTC) Organization: Norwegian university of science and technology Message-ID: References: NNTP-Posting-Host: kiuk0156.chembio.ntnu.no X-Trace: tyfon.itea.ntnu.no 1005130849 18860 129.241.83.82 (7 Nov 2001 11:00:50 GMT) X-Complaints-To: usenet@itea.ntnu.no NNTP-Posting-Date: Wed, 7 Nov 2001 11:00:49 +0000 (UTC) User-Agent: slrn/0.9.7.2 (Linux) Xref: archiver1.google.com comp.lang.ada:15982 Date: 2001-11-07T11:00:49+00:00 List-Id: On Tue, 6 Nov 2001 20:44:19 -0800 (PST), Eric Merritt wrote: > As some of you may know I am fairly new to Ada and I > just found out something that surprised me. I declared > two subtypes of an integer. I was then able to add > these two types together and assign the result to an > instance of one of the above subtypes. or > > subtype type_1 is Integer; > subtype type_2 is Integer; > > type_1_instance : type_1; > type_2_instance : type_2; > type_1_instance_2 : type_1; > > type_1_instance := 1; > type_2_instance := 2; > type_1_instance_2 := type_1_instance + > type_2_instance; > > The addition operator is taking two disparate but > related types and adding them. This is fine, > up-casting is a normal thing. However, when the result > is returned it is not (should not be?) an instance of > the subtype. It seems that the compiler is > down-casting automatically. This bothers me and it > seams that is violates Ada's strong typing. What am I > missing here? Use type not subtype if you do not want the above behavior. Read below (taken from: http://goanna.cs.rmit.edu.au/~dale/ada/aln/4_basic_types.htmli ) Subtypes We can restrict the range of values a variable can take by declaring a subtype with a restricted range of values (this corresponds to Pascal's user defined types). Any attempt to place an out-of-range value into a variable of a subtype results in an exception (program error). In this way program errors can be discovered. The syntax for a subtype declaration is subtype Name is Base_Type; subtype Name is Base_Type range lowerbound . . upperbound; Examples of declaring subtypes are given below. type Processors is (M68000, i8086, i80386, M68030, Pentium, PowerPC); subtype Old_Processors is Processors range M68000..i8086; subtype New_Processors is Processors range Pentium..PowerPC; subtype Data is Integer; subtype Age is Data range 0 . . 140; subtype Temperatures is Float range -50.0 .. 200.0; subtype Upper_Chars is Character range 'A' .. 'Z'; Subtypes are compatable with their base types . They can be placed in the same place as any variable of the base type can. Also variables of different subtypes that are derived from the same base type are compatable. My_Age : Age; Height : Integer; Height := My_Age; -- silly, but never causes a problem. My_Age := Height; -- will cause a problem if height's -- value is outside the range of -- my_age (0..140), but still -- compilable. Preben -- Please, stop bombing civilians in Afghanistan. One cannot write off killing innocent children and other civilians as "collateral damage". A civilian is a civilian whether he or she is American or from another country in the world. http://web.amnesty.org/11september.htm