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 autolearn=unavailable autolearn_force=no version=3.4.4 X-Received: by 2002:ad4:55e8:: with SMTP id bu8mr1934153qvb.61.1579859246796; Fri, 24 Jan 2020 01:47:26 -0800 (PST) X-Received: by 2002:a9d:3d0a:: with SMTP id a10mr2036951otc.327.1579859246512; Fri, 24 Jan 2020 01:47:26 -0800 (PST) Path: eternal-september.org!reader01.eternal-september.org!feeder.eternal-september.org!news.gegeweb.eu!gegeweb.org!usenet-fr.net!proxad.net!feeder1-2.proxad.net!209.85.160.216.MISMATCH!g89no5602778qtd.0!news-out.google.com!o19ni791qtr.1!nntp.google.com!g89no5602772qtd.0!postnews.google.com!google-groups.googlegroups.com!not-for-mail Newsgroups: comp.lang.ada Date: Fri, 24 Jan 2020 01:47:26 -0800 (PST) In-Reply-To: Complaints-To: groups-abuse@google.com Injection-Info: google-groups.googlegroups.com; posting-host=136.163.208.2; posting-account=HFCrOQoAAABZD_f-UUbYHm3lJDIrh-UX NNTP-Posting-Host: 136.163.208.2 References: <6dbb0da9-8c68-435e-945d-d0e1eefeda4c@googlegroups.com> User-Agent: G2/1.0 MIME-Version: 1.0 Message-ID: Subject: Re: Calling a record type's methods (functions or procedure) when record is in an array From: joakimds@kth.se Injection-Date: Fri, 24 Jan 2020 09:47:26 +0000 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable Xref: reader01.eternal-september.org comp.lang.ada:57931 Date: 2020-01-24T01:47:26-08:00 List-Id: Den torsdag 23 januari 2020 kl. 17:51:22 UTC+1 skrev Simon Wright: > joakimds@kth.se writes: >=20 > > Other issues? >=20 > Alignment? >=20 > =3D=3D=3D=3D=3D=3D=3D=3D=3D >=20 > Looks to me like something it would be better not to do, even if you can. Hi Simon, Not entirely sure how Alignment can interfere but I can realize that using = controlled types in the Taft type defined in the body may yield unexpected = results. Right now I am leaning towards better not to do even if one can. H= owever, here is another implementation that may be less error prone: package Cars is type Any_Holder is abstract tagged limited null record; =20 function Make_Holder return Any_Holder'Class; =20 type Any_Car (<>) is limited private; -- This type is defined with unknown discriminant in order to make -- sure instances are properly initialized by allocator function. =20 function Allocate_Car (Holder : in out Any_Holder'Class) return Any_Car; -- The allocator function. type Car_Passenger_Count is range 0 .. 5; =20 function Passenger_Count (Car : Any_Car) return Car_Passenger_Count; procedure Set_Passenger_Count (Car : in out Any_Car; Value : Car_Passenger_Count); private type Taft_Car; type Any_Car (Reference : not null access Taft_Car) is limited null record; end Cars; package body Cars is type Taft_Car is record Passenger_Count : Car_Passenger_Count; end record; type Car_Holder is new Any_Holder with record Car : aliased Taft_Car; end record; =20 function Make_Holder return Any_Holder'Class is begin return C : Car_Holder do C.Car.Passenger_Count :=3D 0; end return; end Make_Holder; function Allocate_Car (Holder : in out Any_Holder'Class) return Any_Car = is H : Car_Holder renames Car_Holder (Holder); begin return Car : Any_Car (H.Car'Access) do null; end return; end Allocate_Car; function Passenger_Count (Car : Any_Car) return Car_Passenger_Count is begin return Car.Reference.Passenger_Count; end Passenger_Count; procedure Set_Passenger_Count (Car : in out Any_Car; Value : Car_Passenger_Count) is begin Car.Reference.all.Passenger_Count :=3D Value; end Set_Passenger_Count; =20 end Cars; with Ada.Text_IO; with Cars; use Cars; procedure Main is Holder : Cars.Any_Holder'Class :=3D Cars.Make_Holder; Car : Cars.Any_Car :=3D Cars.Allocate_Car (Holder); begin Set_Passenger_Count (Car, 3); Ada.Text_IO.Put_Line (Passenger_Count (Car)'Image); end Main; The issues with the above implementation is: 1. Usage of downward conversion at "H : Car_Holder renames Car_Holder (Hold= er);". It is a feature of Ada that can lead to inefficient code generation = and there is a gnatcheck or AdaControl rule to check/forbid such usage. 2. Usage of anonymous access type "(Reference : not null access Taft_Car)".= It isn't obvious from the code as it is written that it is correct althoug= h it is accepted by the GNAT compiler due to the complexity of the anonymou= s access type rules. Perhaps somebody in the Ada community would like to mo= tivate using the anonymous access type rules in the Reference Manual why it= is correct? :) Best regards, Joakim