From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.5-pre1 (2020-06-20) on ip-172-31-74-118.ec2.internal X-Spam-Level: X-Spam-Status: No, score=-1.9 required=3.0 tests=BAYES_00 autolearn=ham autolearn_force=no version=3.4.5-pre1 Path: eternal-september.org!reader02.eternal-september.org!aioe.org!5WHqCw2XxjHb2npjM9GYbw.user.gioia.aioe.org.POSTED!not-for-mail From: "Dmitry A. Kazakov" Newsgroups: comp.lang.ada Subject: Re: Simple example on interfaces Date: Tue, 26 Jan 2021 09:55:03 +0100 Organization: Aioe.org NNTP Server Message-ID: References: <9e1b5d67-be08-4f53-aadc-fbed761a8c24n@googlegroups.com> NNTP-Posting-Host: 5WHqCw2XxjHb2npjM9GYbw.user.gioia.aioe.org Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 7bit X-Complaints-To: abuse@aioe.org User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:78.0) Gecko/20100101 Thunderbird/78.6.1 X-Notice: Filtered by postfilter v. 0.9.2 Content-Language: en-US Xref: reader02.eternal-september.org comp.lang.ada:61205 List-Id: On 2021-01-26 09:17, Mario Blunk wrote: > thanks for your help. I reordered things in the example. The outcome can be seen here: > https://github.com/Blunk-electronic/ada_training/tree/master/src/interfaces > > But as you already said, p2 can not be a real record element as we can see in > https://github.com/Blunk-electronic/ada_training/blob/master/src/interfaces/lib/pac_1.adb > There is no source to fetch the "content" of p2 as there is no element p2. > > Perhaps I did not understand your approach completely. You must provide an implementation for P2: type A2 is new A1 and P2_Interface with private; overriding function P2 (Object : A2) return Enum; type B1 is new Base and P2_Interface with private; overriding function P2 (Object : B1) return Enum; private type A2 is new A1 and P2_Interface with record P2_Implementation : Enum; end record; type B1 is new Base and P2_Interface with record P2_Implementation : Enum; end record; You cannot re-use this implementation. Here you re-use the interface only. There are tricks to re-use implementation as well. As I said I would not recommend them. Anyway, for example, this is how generics can be used to hang an interface on a type. generic type Victim is abstract new Base with private; package Generic_Add_P2 is type Victim_With_P2 is abstract new Victim and P2_Interface with private; overriding function P2 (Object : Victim_With_P2) return Enum; private type Victim_With_P2 is new Victim and P2_Interface with record P2_Implementation : Enum; end record; end Generic_Add_P2; package body Generic_Add_P2 is function P2 (Object : Victim_With_P2) return Enum is begin return Object.P2_Implementation; end P2; end Generic_Add_P2; package Do_A2 is new Generic_Add_P2 (A1); type A2 is new Do_A2.Victim_With_P2 with record -- other members end record; package Do_B1 is new Generic_Add_P2 (Base); type B1 is new Do_B1.Victim_With_P2 with record -- other members end record; You can extend that to have a setter as well. It might even be possible to achieve the getter/setter's syntax to X := A2.P2; A2.P2 := Low; rather than X := A2.P2; Set (A2, Low); But I am too lazy to figure it out, as it would require dozens of helper and access types. I never use this horrific new Ada 2012+ stuff. Maybe someone else might help... -- Regards, Dmitry A. Kazakov http://www.dmitry-kazakov.de