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:a24:e387:: with SMTP id d129mr5207677ith.40.1546732219244; Sat, 05 Jan 2019 15:50:19 -0800 (PST) X-Received: by 2002:aca:4eca:: with SMTP id c193mr698523oib.7.1546732219060; Sat, 05 Jan 2019 15:50:19 -0800 (PST) Path: eternal-september.org!reader01.eternal-september.org!feeder.eternal-september.org!news.uzoreto.com!newsreader4.netcologne.de!news.netcologne.de!peer03.ams1!peer.ams1.xlned.com!news.xlned.com!peer03.am4!peer.am4.highwinds-media.com!peer01.iad!feed-me.highwinds-media.com!news.highwinds-media.com!k10no454235itk.0!news-out.google.com!v141ni351ita.0!nntp.google.com!q69no453299itb.0!postnews.google.com!glegroupsg2000goo.googlegroups.com!not-for-mail Newsgroups: comp.lang.ada Date: Sat, 5 Jan 2019 15:50:18 -0800 (PST) In-Reply-To: Complaints-To: groups-abuse@google.com Injection-Info: glegroupsg2000goo.googlegroups.com; posting-host=70.109.61.2; posting-account=QF6XPQoAAABce2NyPxxDAaKdAkN6RgAf NNTP-Posting-Host: 70.109.61.2 References: <2a6929c5-72fa-4d84-953a-44ea4597ab38@googlegroups.com> <9e6b4219-d6ba-4c89-814d-5ea6e48ed8ea@googlegroups.com> User-Agent: G2/1.0 MIME-Version: 1.0 Message-ID: <9179093f-4765-47a9-9dc6-147c9d7d6c56@googlegroups.com> Subject: Re: class wide iterable (and indexable) From: Jere Injection-Date: Sat, 05 Jan 2019 23:50:19 +0000 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Received-Bytes: 6281 X-Received-Body-CRC: 3641595645 Xref: reader01.eternal-september.org comp.lang.ada:55207 Date: 2019-01-05T15:50:18-08:00 List-Id: 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: > > > > > > Besides linking various type hierarchies together, interfaces are g= ood for containing common code that does not care about specific data stora= ge details. > > >=20 > > > No, interfaces cannot contain common code except for class-wide one.= =20 > > Yes, I have meant the class-wide methods here of course. Although I mus= t admit I wished more than once that non null/abstract methods that could b= e 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 fu= nctionality that could be overriden in a sane way, removing the need for v= arious tricks, one of which you show in your comment.=20 >=20 > You can using a Mixin Generic along with the interface. Consider the > following example: > And if you don't like the Mixin method, you can do something a bit closer t= o 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 =20 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; =20 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; =20 end Some_Interface; =20 package body Some_Interface is =20 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; =20 end Some_Interface; =20 -- For examples using existing inheritance trees we need -- a base type type Third_Party_Base is tagged null record; =20 package Examples is =20 use Some_Interface; =20 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; =20 type Example_2 is new Third_Party_Base and The_Interface with priva= te; overriding function f1(Self : Example_2) return Integer; overriding function f2(Self : Example_2) return Integer; overriding function f3(Self : Example_2) return Integer; =20 private =20 ----------------------------------------------- -- Full Type Declarations ----------------------------------------------- =20 type Example_1 is limited new The_Interface with record The_Interface_Impl : The_Default; end record; =20 type Example_2 is new Third_Party_Base and The_Interface with recor= d The_Interface_Impl : The_Default; end record; =20 end Examples; =20 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); =20 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.