comp.lang.ada
 help / color / mirror / Atom feed
From: "Dmitry A. Kazakov" <mailbox@dmitry-kazakov.de>
Subject: Re: Simple example on interfaces
Date: Tue, 26 Jan 2021 09:55:03 +0100	[thread overview]
Message-ID: <ruold6$sum$1@gioia.aioe.org> (raw)
In-Reply-To: e835f5f4-5904-4c24-a43c-77657175329bn@googlegroups.com

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

  reply	other threads:[~2021-01-26  8:55 UTC|newest]

Thread overview: 28+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-01-25 16:08 Simple example on interfaces Mario Blunk
2021-01-25 16:41 ` Dmitry A. Kazakov
2021-01-25 17:51   ` Mario Blunk
2021-01-25 22:06     ` Dmitry A. Kazakov
2021-01-26  7:33       ` G.B.
2021-01-26  8:07         ` Dmitry A. Kazakov
2021-01-26  8:17           ` Mario Blunk
2021-01-26  8:55             ` Dmitry A. Kazakov [this message]
2021-01-26  9:37       ` J-P. Rosen
2021-01-26 10:25         ` Dmitry A. Kazakov
2021-01-26 11:15           ` AdaMagica
2021-01-26 11:53             ` Dmitry A. Kazakov
2021-01-26 16:46               ` AdaMagica
2021-01-26 19:44                 ` Dmitry A. Kazakov
2021-01-26 20:04                   ` Shark8
2021-01-26 21:34                     ` Dmitry A. Kazakov
2021-01-27  3:11                     ` Randy Brukardt
2021-01-27 22:51                       ` Shark8
2021-01-30  8:33                         ` Randy Brukardt
2021-01-27  3:09                   ` Randy Brukardt
2021-01-27  8:05                     ` Dmitry A. Kazakov
2021-01-26 10:02     ` Stephen Leake
2021-01-25 17:00 ` Jeffrey R. Carter
2021-01-27  1:48   ` philip...@gmail.com
2021-01-27  8:06     ` Dmitry A. Kazakov
2021-01-27  3:36   ` Randy Brukardt
2021-01-27 23:04     ` Shark8
2021-01-25 19:05 ` Stephen Leake
replies disabled

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox