comp.lang.ada
 help / color / mirror / Atom feed
From: Jere <jhb.chat@gmail.com>
Subject: Re: class wide iterable (and indexable)
Date: Sat, 5 Jan 2019 15:50:18 -0800 (PST)
Date: 2019-01-05T15:50:18-08:00	[thread overview]
Message-ID: <9179093f-4765-47a9-9dc6-147c9d7d6c56@googlegroups.com> (raw)
In-Reply-To: <e6b33db7-cf70-49a4-b352-b930a9d9c5b5@googlegroups.com>

On Saturday, January 5, 2019 at 6:29:11 PM UTC-5, Jere wrote:
> On Friday, January 4, 2019 at 7:20:27 AM UTC-5, George Shapovalov wrote:
> > <SNIPPED>
> > > > Besides linking various type hierarchies together, interfaces are good for containing common code that does not care about specific data storage details.
> > > 
> > > No, interfaces cannot contain common code except for class-wide one. 
> > Yes, I have meant the class-wide methods here of course. Although I must admit I wished more than once that non null/abstract methods that could be inherited and overriden are allowed.  Of course they could only reference other methods (as the data is non-existent at this point), all eventually operating on the abstract/null primitives that are essentially "axioms" of the specific interface. But thus one could provide meaningful abstracted functionality that could be overriden in a sane way, removing the need for  various tricks, one of which you show in your comment. 
> 
> You can using a Mixin Generic along with the interface.  Consider the
> following example:
>   <SNIPPED>

And if you don't like the Mixin method, you can do something a bit closer to
composition and Rust's model (though Ada doesn't support traits, so
it has to be emulated by inheriting an interface instead):

*******************************************

with Ada.Text_IO; use Ada.Text_IO;
procedure Hello is

    package Some_Interface is
    
        type The_Interface is limited interface;
        function f1(Self : The_Interface) return Integer is abstract;
        function f2(Self : The_Interface) return Integer is abstract;
        function f3(Self : The_Interface) return Integer is abstract;
        
        type The_Default is new The_Interface with null record;
        overriding function f1(Self : The_Default) return Integer;
        overriding function f2(Self : The_Default) return Integer;
        overriding function f3(Self : The_Default) return Integer;
        
    end Some_Interface;
    
    package body Some_Interface is
        
        function f1(Self : The_Default) return Integer is
        begin
            return 10;
        end f1;
        function f2(Self : The_Default) return Integer is
        begin
            return 20;
        end f2;
        function f3(Self : The_Default) return Integer is
        begin
            return Self.f1 + Self.f2;
        end f3;
        
    end Some_Interface;
    
    -- For examples using existing inheritance trees we need
    -- a base type
    type Third_Party_Base is tagged null record;
    
    package Examples is
    
        use Some_Interface;
    
        type Example_1 is limited new The_Interface with private;
        overriding function f1(Self : Example_1) return Integer;
        overriding function f2(Self : Example_1) return Integer;
        overriding function f3(Self : Example_1) return Integer;
        
        type Example_2 is new Third_Party_Base and The_Interface with private;
        overriding function f1(Self : Example_2) return Integer;
        overriding function f2(Self : Example_2) return Integer;
        overriding function f3(Self : Example_2) return Integer;
        
    private
        
        -----------------------------------------------
        -- Full Type Declarations
        -----------------------------------------------
        
        type Example_1 is limited new The_Interface with record
            The_Interface_Impl : The_Default;
        end record;
        
        type Example_2 is new Third_Party_Base and The_Interface with record
            The_Interface_Impl : The_Default;
        end record;
    
    end Examples;
    
    package body Examples is
        function f1(Self : Example_1) return Integer is (Self.The_Interface_Impl.f1);
        function f2(Self : Example_1) return Integer is (Self.The_Interface_Impl.f2);
        function f3(Self : Example_1) return Integer is (Self.The_Interface_Impl.f3);
        
        function f1(Self : Example_2) return Integer is (Self.The_Interface_Impl.f1);
        function f2(Self : Example_2) return Integer is (Self.The_Interface_Impl.f2);
        function f3(Self : Example_2) return Integer is (Self.The_Interface_Impl.f3);
    end Examples;

begin
    Put_Line("Hello, world!");
end Hello;

*******************************************

This requires more footwork, but enforces that all
implementors of the interface have to specify a body for all
inherited abstract operations.

  reply	other threads:[~2019-01-05 23:50 UTC|newest]

Thread overview: 76+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-01-02 15:48 class wide iterable (and indexable) George Shapovalov
2019-01-02 17:39 ` Simon Wright
2019-01-02 18:11   ` George Shapovalov
2019-01-03  8:52     ` Simon Wright
2019-01-03  9:30       ` George Shapovalov
2019-01-03 16:45         ` Jeffrey R. Carter
2019-01-04  4:32       ` Shark8
2019-01-05  9:03         ` Randy Brukardt
2019-01-03 22:56     ` Randy Brukardt
2019-01-04  0:00       ` George Shapovalov
2019-01-04  8:43         ` Dmitry A. Kazakov
2019-01-04 12:20           ` George Shapovalov
2019-01-05 23:29             ` Jere
2019-01-05 23:50               ` Jere [this message]
2019-01-06  9:34                 ` George Shapovalov
2019-01-06 10:19                   ` Dmitry A. Kazakov
2019-01-06 11:30                     ` George Shapovalov
2019-01-06 12:45                       ` Dmitry A. Kazakov
2019-01-06 13:18                         ` George Shapovalov
2019-01-06 14:13                           ` Dmitry A. Kazakov
2019-01-06 16:33                             ` George Shapovalov
2019-01-06 18:29                               ` George Shapovalov
2019-01-06 20:32                                 ` Dmitry A. Kazakov
2019-01-06 21:47                                   ` George Shapovalov
2019-01-07  9:37                                     ` Niklas Holsti
2019-01-07 16:24                                       ` George Shapovalov
2019-01-06 20:18                               ` Dmitry A. Kazakov
2019-01-06 21:58                                 ` George Shapovalov
2019-01-07  8:28                                   ` Dmitry A. Kazakov
2019-01-05  9:21           ` Randy Brukardt
2019-01-05 10:07             ` Dmitry A. Kazakov
2019-01-05 18:17               ` George Shapovalov
2019-01-05 20:07                 ` Simon Wright
2019-01-05 20:41                   ` George Shapovalov
2019-01-07 21:07               ` Randy Brukardt
2019-01-08  9:51                 ` Dmitry A. Kazakov
2019-01-08 19:25                   ` Björn Lundin
2019-01-08 23:26                   ` Randy Brukardt
2019-01-09 17:06                     ` Dmitry A. Kazakov
2019-01-09 23:38                       ` Randy Brukardt
2019-01-10  8:53                         ` Dmitry A. Kazakov
2019-01-10 22:14                           ` Randy Brukardt
2019-01-11  9:09                             ` Dmitry A. Kazakov
2019-01-14 22:59                               ` Randy Brukardt
2019-01-15  9:34                                 ` Dmitry A. Kazakov
2019-01-18 15:48                                   ` Olivier Henley
2019-01-18 16:08                                     ` Dmitry A. Kazakov
2019-01-18 16:29                                       ` Olivier Henley
2019-01-18 16:54                                         ` Dmitry A. Kazakov
2019-01-18 17:31                                           ` Olivier Henley
2019-01-18 18:51                                             ` Shark8
2019-01-18 20:09                                             ` Dmitry A. Kazakov
2019-01-21 23:15                                     ` Randy Brukardt
2019-01-22  8:56                                       ` Dmitry A. Kazakov
2019-01-22 22:00                                         ` Randy Brukardt
2019-01-23  8:14                                           ` Dmitry A. Kazakov
2019-01-22 17:04                                       ` Jeffrey R. Carter
2019-01-22 22:02                                         ` Randy Brukardt
2019-01-23 18:00                                           ` Jeffrey R. Carter
2019-01-23 20:14                                           ` Shark8
2019-01-23 22:47                                             ` Randy Brukardt
2019-01-24 17:11                                               ` Dmitry A. Kazakov
2019-01-28 15:54                                               ` Shark8
2019-01-28 17:23                                                 ` Dmitry A. Kazakov
2019-01-08 18:32                 ` G. B.
2019-01-05 17:05             ` Jeffrey R. Carter
2019-01-05 20:18               ` Dmitry A. Kazakov
2019-01-05 21:09               ` Shark8
2019-01-06 10:11                 ` Jeffrey R. Carter
2019-01-05 20:46             ` Shark8
2019-01-06  9:43               ` Dmitry A. Kazakov
2019-01-26 22:11 ` George Shapovalov
2019-01-26 22:14   ` George Shapovalov
  -- strict thread matches above, loose matches on Subject: below --
2019-01-29  7:45 Randy Brukardt
2019-01-29 19:34 ` Niklas Holsti
2019-01-29 20:26   ` Dmitry A. Kazakov
replies disabled

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