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,FREEMAIL_FROM autolearn=unavailable autolearn_force=no version=3.4.4 Path: eternal-september.org!reader01.eternal-september.org!feeder.eternal-september.org!news.uzoreto.com!fu-berlin.de!uni-berlin.de!individual.net!not-for-mail From: hreba Newsgroups: comp.lang.ada Subject: Array Index: substitute integer with enumeration Date: Thu, 4 Jun 2020 18:54:54 +0200 Message-ID: Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 7bit X-Trace: individual.net P+8hf0TqEqE3ETLOMabJjwrW7zqu4UIZTLe5LRg81KAl9FlbGm Cancel-Lock: sha1:hfCRSQ8vMNE8O6WwDuoRyYnSfLg= X-Mozilla-News-Host: news://News.Individual.DE:119 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:68.0) Gecko/20100101 Thunderbird/68.7.0 Content-Language: en-US Xref: reader01.eternal-september.org comp.lang.ada:58959 Date: 2020-06-04T18:54:54+02:00 List-Id: 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