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 autolearn=unavailable autolearn_force=no version=3.4.4 Path: eternal-september.org!reader01.eternal-september.org!reader02.eternal-september.org!feeder.eternal-september.org!aioe.org!.POSTED!not-for-mail From: "Dmitry A. Kazakov" Newsgroups: comp.lang.ada Subject: Re: troubles learning OOP: expected type "Parent.Some_Child", found type Parent'Class Date: Tue, 13 Feb 2018 18:56:09 +0100 Organization: Aioe.org NNTP Server Message-ID: References: <634e9064-33f8-42c0-9032-e630f98c89d4@googlegroups.com> <982639bc-8e7e-4c43-a1ff-cf9fa7c0d0ed@googlegroups.com> NNTP-Posting-Host: 9s2HXjYgtb87Rt4JbNw44Q.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; WOW64; rv:52.0) Gecko/20100101 Thunderbird/52.6.0 X-Notice: Filtered by postfilter v. 0.8.3 Content-Language: en-US Xref: reader02.eternal-september.org comp.lang.ada:50425 Date: 2018-02-13T18:56:09+01:00 List-Id: On 2018-02-13 18:42, Jeffrey R. Carter wrote: > On 02/12/2018 10:18 PM, Mehdi Saada wrote: >> >> I can't believe T_TOKEN'class is as useless... well, in a sense, since >> the program doesn't know the actual type of TOKEN but only that it's a >> child of T_TOKEN, no wonder there's some limitations. But I'm >> surprised there's no mechanism in the language, to tell the program: >> HEY there ! I'm sure it's T_TOKEN_OPERANDE, so now consider TOKEN as >> such and let me reach it's OPERANDE component. > > Given an object V of T_Token'Class, you can do > > if V in T_Token_Operateur > > and > > if V in T_Token_Operateur'Class > > and then do a conversion to access the components of the extension. You > can also compare V'Tag to the tags of the derived types. All of these > are frowned on as "not the type-extension way" by aficionados of type > extension. Right. It is bad to expose type-specific components. This why Ada offers type S is new T with private; to ensure that clients cannot access internals of the type otherwise than through public operations which normally should be operations defined on the whole class and thus accessible through dispatch without any type conversions and explicit tests. P.S. There are few cases when Ada design fails and there is a need to convert away from the root type. E.g. in class-wide construction functions when a specific object is returned as a class-wide. The pattern is to create a result and then convert to the specific type to initialize. -- Regards, Dmitry A. Kazakov http://www.dmitry-kazakov.de