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 X-Received: by 2002:a05:6214:1428:: with SMTP id o8mr18202930qvx.87.1579810538093; Thu, 23 Jan 2020 12:15:38 -0800 (PST) X-Received: by 2002:a9d:3d0a:: with SMTP id a10mr58758otc.327.1579810537749; Thu, 23 Jan 2020 12:15:37 -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!g89no3725515qtd.0!news-out.google.com!w29ni118qtc.0!nntp.google.com!g89no3725509qtd.0!postnews.google.com!google-groups.googlegroups.com!not-for-mail Newsgroups: comp.lang.ada Date: Thu, 23 Jan 2020 12:15:37 -0800 (PST) In-Reply-To: Complaints-To: groups-abuse@google.com Injection-Info: google-groups.googlegroups.com; posting-host=47.185.239.228; posting-account=zwxLlwoAAAChLBU7oraRzNDnqQYkYbpo NNTP-Posting-Host: 47.185.239.228 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: Optikos Injection-Date: Thu, 23 Jan 2020 20:15:38 +0000 Content-Type: text/plain; charset="UTF-8" Xref: reader01.eternal-september.org comp.lang.ada:57928 Date: 2020-01-23T12:15:37-08:00 List-Id: On Thursday, January 23, 2020 at 9:00:18 AM UTC-6, joak...@kth.se wrote: > > 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 Background on "Taft types" (a.k.a. Ichbiah's name "Taft-amendment types" or the generic/language-independent name "opaque pointers" or the C++ jargon "pimpl"). https://groups.google.com/forum/#!msg/comp.lang.ada/KelH3y5WzEA/z3pyInm_hqgJ