From: cgreen@yosemite.atc.com (Christopher Green)
Subject: Re: assignments using different types
Date: 1997/01/28
Date: 1997-01-28T00:00:00+00:00 [thread overview]
Message-ID: <5clkbv$a3k@newshub.atmnet.net> (raw)
In-Reply-To: 32EE4431.6D24@ti.com
In article <32EE4431.6D24@ti.com>, David Dessert <d-dessert@ti.com> 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
next prev parent reply other threads:[~1997-01-28 0:00 UTC|newest]
Thread overview: 10+ messages / expand[flat|nested] mbox.gz Atom feed top
1997-01-28 0:00 assignments using different types David Dessert
1997-01-28 0:00 ` Christopher Green [this message]
1997-01-29 0:00 ` Robert I. Eachus
1997-01-29 0:00 ` Robert Dewar
1997-01-29 0:00 ` Robert Dewar
1997-01-30 0:00 ` inter-unit inlining (was: Re: assignments using different types) Fergus Henderson
1997-01-29 0:00 ` assignments using different types Paul Van Bellinghen
1997-02-10 0:00 ` Robert Dewar
-- strict thread matches above, loose matches on Subject: below --
1997-01-29 0:00 tmoran
1997-01-30 0:00 Thomas
replies disabled
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox