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=-0.9 required=5.0 tests=BAYES_00,FORGED_GMAIL_RCVD, FREEMAIL_FROM autolearn=no autolearn_force=no version=3.4.4 X-Received: by 2002:aed:3169:: with SMTP id 96mr28822304qtg.211.1591723536103; Tue, 09 Jun 2020 10:25:36 -0700 (PDT) X-Received: by 2002:a4a:4c8e:: with SMTP id a136mr22023535oob.23.1591723535860; Tue, 09 Jun 2020 10:25:35 -0700 (PDT) Path: eternal-september.org!reader01.eternal-september.org!feeder.eternal-september.org!aioe.org!peer01.ams4!peer.am4.highwinds-media.com!peer03.iad!feed-me.highwinds-media.com!news.highwinds-media.com!news-out.google.com!nntp.google.com!postnews.google.com!google-groups.googlegroups.com!not-for-mail Newsgroups: comp.lang.ada Date: Tue, 9 Jun 2020 10:25:35 -0700 (PDT) In-Reply-To: Complaints-To: groups-abuse@google.com Injection-Info: google-groups.googlegroups.com; posting-host=146.5.2.231; posting-account=lJ3JNwoAAAAQfH3VV9vttJLkThaxtTfC NNTP-Posting-Host: 146.5.2.231 References: User-Agent: G2/1.0 MIME-Version: 1.0 Message-ID: Subject: Re: Array Index: substitute integer with enumeration From: Shark8 Injection-Date: Tue, 09 Jun 2020 17:25:36 +0000 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Received-Bytes: 4545 X-Received-Body-CRC: 1019651907 Xref: reader01.eternal-september.org comp.lang.ada:59025 Date: 2020-06-09T10:25:35-07:00 List-Id: On Thursday, June 4, 2020 at 10:54:57 AM UTC-6, hreba wrote: > Just as an example, let's assume there is a general geometry package=20 > which requires a function from the client which transforms cartesian=20 > coordinates into a client-specific coordinate system: >=20 > type Point is array (range 1..3) of Float; >=20 > type Transform_T is access Function (p: Point) return Point; You don't need accesses in any of your program-facing compilation-units; yo= u *might* for the library-facing interfaces. The type Point can also be mod= eled differently for the program-facing side and the library-facing side. = =E2=80=94 That is, you can do everything you need to interface the library = in the private-section and/or body (body would arguably be preferable, beca= use if you discard the library's usage, you've constrained the library only= to the body and thus only have one thing to change. Using coordinates + enumerations you could, perhaps, use: Type Point is record r, phi, theta: Interfaces.IEEE_Float_32; End Record with Size =3D> Interfaces.IEEE_Float_32*3; and then in your library interfacing part, have: Type Library_Point is Array(1..3) of Interfaces.IEEE_Float_32 with Convention =3D> Fortran, Size =3D> Interfaces.IEEE_Float_32*3; Pragma Assert( Point'Size =3D Library_Point'Size, "Point-type sizes must match." ); -- Imported functions. -- To_Cartesian and To_Spherical, etc. =20 Function Convert( Input : Point ) return Library_Point is -- Isolating the Unchecked Conversion. Function Cvt is new Ada.Unchecked_Conversion(Point, Library_Point); Begin Return Result : constant Library_Point :=3D To_Cartesian( Cvt(Input)= ); End Convert; =20 Function Convert( Input : Library_Point ) return Point is -- Isolating the Unchecked Conversion. Function Cvt is new Ada.Unchecked_Conversion(Library_Point, Point); Begin Return Result : constant Library_Point :=3D To_Spherical( Cvt(Input)= ); End Convert; and take your conversion functions To_Cartesian and To_Spherical and passing their accesses to the importe= d functions that need them. TL;DR =E2=80=94 Make the interface you want to present to your program firs= t then, in the body, do all your interfacing and library-importing. > My questions are now: >=20 > 1. Does the initialization of pc involve any copying at run time? IIRC, parameters are passed as reference for arrays and records; so if that= 's what you're using to model a point, it shouldn't for functions. Initiali= zation, especially with aggregates, is a different matter and one I'm less = sure on. (I seem to recall someone, Lucretia?, doing some work with GNAT an= d coming to the conclusion that if there was an "Others" in the aggregate t= here would be a loop generated, which might have copying.) > 2. If so, how can it be avoided? Usually by things like shown: isolate your critical functions, and then (a)= present an interface for the rest of the program to use while (b) ensuring= those critical functions behave as you want. (This might take a little mor= e expertise on the subject, as it sounds like you could be messing with emb= edded and my experience there is extremely limited.)