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.3 required=5.0 tests=BAYES_00,INVALID_MSGID autolearn=no autolearn_force=no version=3.4.4 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 103376,a90718b61c5de714 X-Google-Attributes: gid103376,public From: cgreen@yosemite.atc.com (Christopher Green) Subject: Re: assignments using different types Date: 1997/01/28 Message-ID: <5clkbv$a3k@newshub.atmnet.net>#1/1 X-Deja-AN: 212834461 references: <32EE4431.6D24@ti.com> organization: Advanced Technology Center, Laguna Hills, CA newsgroups: comp.lang.ada Date: 1997-01-28T00:00:00+00:00 List-Id: In article <32EE4431.6D24@ti.com>, David Dessert wrote: >I have a question about readability and efficiency of assignments >when using strict types in Ada83. I'm using the TI-Tartan C40 >compiler, which has few Ada95 features. > >Example: > type Distance_Type is new float range 0.0 .. 1000.0; > type Time_Type is new float range 0.0 .. 10.0; > type Velocity_Type is new float; > > Distance : Distance_Type := 100.0; > Time : Time_Type := 10.0; > Velocity : Velocity_Type; > >... > > -- A difficult to read, but legal Ada assignment. > Velocity := Velocity_Type(float(Distance) / float(Time)); > > >Can anyone provide me with suggestions to improve the readability >of the above assignment? Of course, I'd like efficiency to be >similar to that of using the same type for all the variables. If your compiler does something useful with "pragma Inline", the following is no less efficient and quite a bit more expressive. I've extended the example to show the expressiveness of using the type system and operators together... type Distance_Type is new float; type Time_Type is new float; type Velocity_Type is new float; type Acceleration_Type is new float; -- note that the inlined functions' names are chosen to be unique: -- this avoids problems with attempting to inline functions that -- have overloaded names. function V_Is_Dx_Dt (Left : in Distance_Type; Right : in Time_Type) return Velocity_Type; pragma Inline (V_Is_Dx_Dt); function A_Is_Dv_Dt (Left : in Velocity_Type; Right : in Time_Type) return Acceleration_Type; pragma Inline (A_Is_Dv_Dt); -- these are so we can invoke our definitions as operators. function "/" (Left : in Distance_Type; Right : in Time_Type) return Velocity_Type renames V_Is_Dx_Dt; function "/" (Left : in Velocity_Type; Right : in Time_Type) return Acceleration_Type renames A_Is_Dv_Dt; -- the type conversions are buried in the function bodies. function V_Is_Dx_Dt (Left : in Distance_Type; Right : in Time_Type) return Velocity_Type is begin return Velocity_Type (float (Left) / float (Right)); end V_Is_Dx_Dt; function A_Is_Dv_Dt (Left : in Velocity_Type; Right : in Time_Type) return Acceleration_Type is begin return Acceleration_Type (float (Left) / float (Right)); end A_Is_Dv_Dt; -- and later on, simply write... Distance : Distance_Type := 100.0; Time : Time_Type := 10.0; Velocity : Velocity_Type; Acceleration : Acceleration_Type; Velocity := Distance / Time; Acceleration := Velocity / Time; Chris Green Email cgreen@atc.com Advanced Technology Center Phone (714) 583-9119 22982 Mill Creek Drive ext. 220 Laguna Hills, CA 92653 Fax (714) 583-9213