comp.lang.ada
 help / color / mirror / Atom feed
From: John McCabe <john@nospam.mccabe.org.uk>
Subject: Re: How to check class type ?
Date: Fri, 23 Apr 2021 16:13:44 -0000 (UTC)	[thread overview]
Message-ID: <s5urno$m4d$1@dont-email.me> (raw)
In-Reply-To: 6080656d$0$3708$426a34cc@news.free.fr

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 <object>.<subprogram> and 
expect <subprogram> 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


  reply	other threads:[~2021-04-23 16:13 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-04-21 17:08 How to check class type ? DrPi
2021-04-21 17:26 ` AdaMagica
2021-04-21 17:33   ` DrPi
2021-04-21 17:48   ` DrPi
2021-04-23 16:13     ` John McCabe [this message]
2021-04-21 17:40 ` Dmitry A. Kazakov
2021-04-22  8:50   ` DrPi
replies disabled

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