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.2 required=5.0 tests=BAYES_00,FROM_WORDY, INVALID_MSGID autolearn=no autolearn_force=no version=3.4.4 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 103376,f127842852d2f03a X-Google-Attributes: gid103376,public From: "Ken Garlington" Subject: Re: About conversions Date: 2000/11/20 Message-ID: #1/1 X-Deja-AN: 695755365 References: <8vb0h9$1ou$1@nnrp1.deja.com> X-Priority: 3 X-MimeOLE: Produced By Microsoft MimeOLE V5.50.4133.2400 X-Complaints-To: abuse@flash.net X-Trace: news.flash.net 974729009 216.215.75.235 (Mon, 20 Nov 2000 08:03:29 CST) Organization: FlashNet Communications, http://www.flash.net X-MSMail-Priority: Normal NNTP-Posting-Date: Mon, 20 Nov 2000 08:03:29 CST Newsgroups: comp.lang.ada Date: 2000-11-20T00:00:00+00:00 List-Id: "Sandro Binetti" wrote in message news:8vb0h9$1ou$1@nnrp1.deja.com... : Hi everybody. : : Can you help me understand the meaning of a type conversion, like: : INTEGER(v) ? : : Take an example like the following. : : ------------------------- : : procedure PROVA is : type TYP is range 1..10; : c:typ; : : procedure P(a,b:in integer;result:out integer) is : begin : result:=a+b; : end p; : : begin -- PROVA : p(10,20,INTEGER(c)); -- <<<< : end prova; : : ---------------------------------------- : : : The compiler doesn't say anything at the point <<<< : : Why? Here's an example that might help: Suppose I need to define a type that represents temperature, from roughly the freezing point of water to its boiling point. If I define something like type Temperature is new Float range 0.0 .. 100.0; I might be right, or I might be wrong, depending upon what units I had in mind. However, if I define something like type Celsius is new Float range 0.0 .. 100.0; type Fahrenheit is new Float range 32.0 .. 212.0; then the units are clear. By defining these as separate types, I can catch certain kinds of errors, such as attempting to display a Celsius value assuming it was Fahrenheit: Thermometer : Celsius; ... procedure Display_Temperature ( Value : in Fahrenheit ); ... Display_Temperature (Thermometer); -- error! Your compiler will object to this attempt. To fix it, you might discover that Thermometer should really be Fahrenheit, or that Display_Temperature should really display Celsius values. However, what happens if Thermometer is really a Celsius thermometer, and the display should show values in Fahrenheit? This can be fixed, of course: I could create a function, such as function To_Fahrenheit (Temperature : Celsius) return Fahrenheit is begin return (9.0/5.0)*Temperature-32.0; -- error! end To_Fahrenheit; and use it to convert the Thermometer to the right scale. Unfortunately, the compiler will complain here, as well. It will assume that you are inadvertantly returning a Celsius value as a Fahrenheit value. To make it clear that the calculation actually causes the value to be of a different type, you have to do an explicit type conversion, as follows: function To_Fahrenheit (Temperature : Celsius) return Fahrenheit is begin return Fahrenheit((9.0/5.0)*Temperature-32.0); -- OK end To_Fahrenheit; As you noticed, the compiler does not complain when you use an explicit type conversion to make a value the expected type. In your example, you have a procedure P that expects values of type Integer, you convert your Typ value to an Integer value before calling P, so everything is consistent. There are common-sense limits to type conversions, of course. See the Ada Reference Manual section 4.6 for details. http://www.adahome.com/LRM/95/rm95html/rm9x-04-06.html package Temperature is type Celsius is new Float range 0.0 .. 100.0; type Fahrenheit is new Float range 32.0 .. 212.0; function To_Celsius (Temperature : Fahrenheit) return Celsius; function To_Fahrenheit (Temperature : Celsius) return Fahrenheit; end Temperature; package body Temperature is function To_Celsius (Temperature : Fahrenheit) return Celsius is begin return Celsius((5.0/9.0)*(Temperature-32.0)); end To_Celsius; function To_Fahrenheit (Temperature : Celsius) return Fahrenheit is begin return Fahrenheit((9.0/5.0)*Temperature-32.0); end To_Fahrenheit; end Temperature;