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:4f47:: with SMTP id eu7mr16227125qvb.69.1579791616606; Thu, 23 Jan 2020 07:00:16 -0800 (PST) X-Received: by 2002:aca:cdd6:: with SMTP id d205mr10554934oig.90.1579791616232; Thu, 23 Jan 2020 07:00:16 -0800 (PST) Path: eternal-september.org!reader01.eternal-september.org!feeder.eternal-september.org!news.uzoreto.com!feeder5.feed.usenet.farm!feed.usenet.farm!tr2.eu1.usenetexpress.com!feeder.usenetexpress.com!tr3.iad1.usenetexpress.com!border1.nntp.dca1.giganews.com!nntp.giganews.com!g89no2912767qtd.0!news-out.google.com!o19ni684qtr.1!nntp.google.com!g89no2912759qtd.0!postnews.google.com!google-groups.googlegroups.com!not-for-mail Newsgroups: comp.lang.ada Date: Thu, 23 Jan 2020 07:00:15 -0800 (PST) In-Reply-To: <6dbb0da9-8c68-435e-945d-d0e1eefeda4c@googlegroups.com> 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: Thu, 23 Jan 2020 15:00:16 +0000 Content-Type: text/plain; charset="UTF-8" Xref: reader01.eternal-september.org comp.lang.ada:57925 Date: 2020-01-23T07:00:15-08:00 List-Id: > And now, you could change the type of the components to Point to Integer, remove the type-casts, and recompile the package Example **without having to recompile dependent packages** because they are dependent only on the visible portion of the package. Using Taft types introduced in Ada95 it is possible to add new components to a record type without recompilation of dependent packages. Here is an Ada 2005 example: private with System.Storage_Elements; package Cars is 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. function Allocate_Car return Any_Car; -- The allocator function. type Car_Passenger_Count is range 0 .. 5; 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 Taft_Car_Ptr is access all Taft_Car; type Any_Car (Offset : System.Storage_Elements.Storage_Offset) is limited record Allocated_Memory : aliased System.Storage_Elements.Storage_Array (1 .. Offset); Reference : Taft_Car_Ptr; end record; end Cars; with System.Address_To_Access_Conversions; package body Cars is type Taft_Car is record Passenger_Count : Car_Passenger_Count; end record; package Conversions is new System.Address_To_Access_Conversions (Object => Taft_Car); function Allocate_Car return Any_Car is Default : constant Any_Car := (Offset => Taft_Car'Max_Size_In_Storage_Elements, Allocated_Memory => (others => 0), Reference => null); begin return Car : Any_Car (Taft_Car'Max_Size_In_Storage_Elements) do declare First_Index : constant System.Storage_Elements.Storage_Offset := Car.Allocated_Memory'First; begin Car.Reference := Conversions.To_Pointer (Car.Allocated_Memory (First_Index)'Address).all'Access; end; 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 := Value; end Set_Passenger_Count; end Cars; with Ada.Text_IO; with Cars; use Cars; procedure Main is Car : Cars.Any_Car := Cars.Allocate_Car; begin Set_Passenger_Count (Car, 3); Ada.Text_IO.Put_Line (Passenger_Count (Car)'Image); end Main; Does somebody have a better implementation of Taft types where heap allocations are not used? Perhaps this example is something to add to documentation on Ada on the internet like for example: https://en.wikibooks.org/wiki/Ada_Programming/Tips Anything wrong or could be improved with the implementation above? Best regards, Joakim