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!.POSTED!not-for-mail From: John McCabe Newsgroups: comp.lang.ada Subject: Re: How to check class type ? Date: Fri, 23 Apr 2021 16:13:44 -0000 (UTC) Organization: A noiseless patient Spider Message-ID: References: <60805c0e$0$12690$426a74cc@news.free.fr> <6080656d$0$3708$426a34cc@news.free.fr> Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Injection-Date: Fri, 23 Apr 2021 16:13:44 -0000 (UTC) Injection-Info: reader02.eternal-september.org; posting-host="d27d0e3741d481035735304f4ab6e775"; logging-data="22669"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18fLyUkLhGB/SzCyXfP4L0MsICfo1JYzpk=" User-Agent: Pan/0.147 (Sweet Solitude; 97d1711 github.com/GNOME/pan.git) Cancel-Lock: sha1:XFFeci5gwn3KAE5CByG8vKBkKQ8= Xref: reader02.eternal-september.org comp.lang.ada:61900 List-Id: On Wed, 21 Apr 2021 19:48:22 +0200, DrPi wrote: > Le 21/04/2021 à 19:26, AdaMagica a écrit : >> DrPi schrieb am Mittwoch, 21. April 2021 um 19:08:32 UTC+2: >>> if var'Tag = Leaf1'Tag then ... >>> elsif var'Tag = Leaf2'Tag then ... >>> end if; >>> >>> It works. But is this correct ? >> >> Why not? >> >> if Var in Leaf1 then >> ... >> elsif Var in Leaf2 then >> >> But normally you would use dispatching. > Mmmh... Makes me think I did it the wrong way... > I'm a C programmer. I'm afraid I still think as if I where programming > in C. > I'll change my code. The tagged type stuff is really there to support polymorphism, and when you have a heterogeneous container, where its contents are objects of, or derived from, a base class, then the general idea is that the client who's using that container shouldn't care about the exact type of the object it's dealing with, it should just call . and expect to be there. While that's all well and good in theory, in practise it's very easy to mess up and, as Dmitry says, end up with a base class that defines multiple different subprograms where derived classes implement a subset of those. In these cases you need to ask yourself where the subclasses really do have an "is a" relationship with the base class, or whether there is some pattern that you can use, e.g. visitor (https:// www.adacore.com/gems/gem-113-visitor-pattern-in-ada - which, unless I'm mistaken, corresponds to Dmitry's comment about multiple dispatch) to avoid having to make those checks. HTH