comp.lang.ada
 help / color / mirror / Atom feed
* Array Index: substitute integer with enumeration
@ 2020-06-04 16:54 hreba
  2020-06-04 17:49 ` Jeffrey R. Carter
  2020-06-09 17:25 ` Shark8
  0 siblings, 2 replies; 5+ messages in thread
From: hreba @ 2020-06-04 16:54 UTC (permalink / raw)


Just as an example, let's assume there is a general geometry package 
which requires a function from the client which transforms cartesian 
coordinates into a client-specific coordinate system:

type Point is array (range 1..3) of Float;

type Transform_T is access Function (p: Point) return Point;

As a client who implements spherical coordinates, I would then write a 
function:

function To_Spherical (p: Point) return Point is
begin
   return
     ( Sqrt(p(1)**2 + p(2)**2 + p(3)**2),	-- r
       Arctan(p(2)/p(1)),			-- phi
       ...
     )
end To_Spherical.

Enumerations would make this less error-prone, but somewhat clumsy. With

type Cart_Coord is (x, y, z);

p(1) could be written as p(x'Pos+1). This is safer, but if I would 
define additionally

  type Spherical_Coord is (r, phi, theta)

the compiler wouldn't notice a mix-up of x'Pos and r'Pos. But now I 
could write

type Cart_Point is array (Cart_Coord) of Float;

function To_Spherical (p: Point) return Point is
   pc: Cart_Point:= p;
begin
   return
     ( Sqrt(pc(x)**2 + pc(y)**2 + pc(z)**2),	-- r
       Arctan(p(y)/p(x)),			-- phi
       ...
     )
end To_Spherical.

My questions are now:

1. Does the initialization of pc involve any copying at run time?
2. If so, how can it be avoided?

-- 
Frank Hrebabetzky, Kronach	+49 / 9261 / 950 0565

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2020-06-09 17:25 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-06-04 16:54 Array Index: substitute integer with enumeration hreba
2020-06-04 17:49 ` Jeffrey R. Carter
2020-06-05 15:16   ` hreba
2020-06-05 17:15     ` Jeffrey R. Carter
2020-06-09 17:25 ` Shark8

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox