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: Mon, 25 Jan 2021 23:06:09 +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:61199 List-Id: On 2021-01-25 18:51, Mario Blunk wrote: > >> You must define "property." What is it? > With "property" I mean the selectors like p0, p1 or p2. They are components or else record members. >> In your code these types have p2 >> already. Is this static polymorphism not enough? > It seems so. Suppose there would be more types like C1, D1, ..., each of which derived from type_base, then > I don't want to define p2 over and over. There are two different things: 1. interface of a type (in general sense) 2. an implementation of Ada interface is a type that has interface and no implementation. [It is a silly idea inherited from Java.] So: - if "define" applies to #1, then yes, you can inherit from Ada interface and thus you do not need to "define" inherited primitive operations like P2 again. - if "define" applies to #2, then no, you cannot inherit implementation from Ada interface because it can have none. The first non-abstract type inheriting from an Ada interface must implement its interface in full. The types derived from it will inherit that implementation. But each sibling as A2 and B1 must implement the interface anew. >> Do you need A2 and B1 >> in a class having p2, with polymorphic objects of the class? Then that >> would indeed be an interface. > How would that look like ? with declarations from the previous post: procedure Print_P2 (Object : P2_Interface'Class) is begin Put ("P2=" & Enum'Image (Object.P2)); end Print_P2; X : A2; Y : B1; begin Print_P2 (X); Print_P2 (Y); >> Now, there is no full multiple inheritance in Ada, if p2 must be a >> component, you are out of luck. You have only one shot and you have >> spent it on p0 in the type Base. > That is forbidden as far as the final application is concerned. The example I posted here is a simplification of a more complex scenario. There exist various dirty tricks to emulate full multiple inheritance but no universal solution. If you really need full multiple inheritance, choose the most important path of implementations and make types along it proper types. Other paths if simple, could tricked using - Min-in inheritance - Generic packages to automate implementation of interfaces - Memory pools to inject implementation Nothing of these is good. They basically work only if the depths of the secondary inheritance paths is 1. -- Regards, Dmitry A. Kazakov http://www.dmitry-kazakov.de