Rocksolid Light

Welcome to novaBBS (click a section below)

mail  files  register  newsreader  groups  login

Message-ID:  

Nature, to be commanded, must be obeyed. -- Francis Bacon


devel / comp.lang.ada / Re: How to check class type ?

SubjectAuthor
* How to check class type ?DrPi
+* Re: How to check class type ?AdaMagica
|+- Re: How to check class type ?DrPi
|`* Re: How to check class type ?DrPi
| `- Re: How to check class type ?John McCabe
`* Re: How to check class type ?Dmitry A. Kazakov
 `- Re: How to check class type ?DrPi

1
How to check class type ?

<60805c0e$0$12690$426a74cc@news.free.fr>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=5094&group=comp.lang.ada#5094

  copy link   Newsgroups: comp.lang.ada
Path: i2pn2.org!i2pn.org!weretis.net!feeder8.news.weretis.net!proxad.net!feeder1-2.proxad.net!cleanfeed3-a.proxad.net!nnrp1-2.free.fr!not-for-mail
Newsgroups: comp.lang.ada
X-Mozilla-News-Host: news://news.free.fr:119
From: 314...@drpi.fr (DrPi)
Subject: How to check class type ?
Date: Wed, 21 Apr 2021 19:08:24 +0200
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:78.0) Gecko/20100101
Thunderbird/78.9.1
MIME-Version: 1.0
Content-Type: text/plain; charset=utf-8; format=flowed
Content-Language: en-US
Content-Transfer-Encoding: 8bit
Lines: 25
Message-ID: <60805c0e$0$12690$426a74cc@news.free.fr>
Organization: Guest of ProXad - France
NNTP-Posting-Date: 21 Apr 2021 19:08:30 CEST
NNTP-Posting-Host: 82.65.30.55
X-Trace: 1619024910 news-2.free.fr 12690 82.65.30.55:52445
X-Complaints-To: abuse@proxad.net
 by: DrPi - Wed, 21 Apr 2021 17:08 UTC

Hi,

I have the following declarations (just for the example) :

type Root is tagged null record;
type Root_Ptr is access all Root'Class;

type Leaf1 is new Root with null record;
type Leaf1_Ptr is access all Leaf1'Class;

type Leaf2 is new Root with null record;
type Leaf2_Ptr is access all Leaf2'Class;

To check if a variable is of type Leaf1_Ptr or Leaf2_Ptr, I do :
if var'Tag = Leaf1'Tag then
...
elsif var'Tag = Leaf2'Tag then
...
end if;

It works. But is this correct ? Should the test be done differently to
suit Ada coding style ?

Nicolas

Re: How to check class type ?

<d66d1eb4-f642-4962-9674-0e70ac2ca537n@googlegroups.com>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=5095&group=comp.lang.ada#5095

  copy link   Newsgroups: comp.lang.ada
X-Received: by 2002:ac8:7fc5:: with SMTP id b5mr22480925qtk.122.1619026010312;
Wed, 21 Apr 2021 10:26:50 -0700 (PDT)
X-Received: by 2002:a25:d6d2:: with SMTP id n201mr16954291ybg.504.1619026010127;
Wed, 21 Apr 2021 10:26:50 -0700 (PDT)
Path: i2pn2.org!i2pn.org!weretis.net!feeder8.news.weretis.net!proxad.net!feeder1-2.proxad.net!209.85.160.216.MISMATCH!news-out.google.com!nntp.google.com!postnews.google.com!google-groups.googlegroups.com!not-for-mail
Newsgroups: comp.lang.ada
Date: Wed, 21 Apr 2021 10:26:49 -0700 (PDT)
In-Reply-To: <60805c0e$0$12690$426a74cc@news.free.fr>
Injection-Info: google-groups.googlegroups.com; posting-host=94.31.102.170; posting-account=rmHyLAoAAADSQmMWJF0a_815Fdd96RDf
NNTP-Posting-Host: 94.31.102.170
References: <60805c0e$0$12690$426a74cc@news.free.fr>
User-Agent: G2/1.0
MIME-Version: 1.0
Message-ID: <d66d1eb4-f642-4962-9674-0e70ac2ca537n@googlegroups.com>
Subject: Re: How to check class type ?
From: christ-u...@t-online.de (AdaMagica)
Injection-Date: Wed, 21 Apr 2021 17:26:50 +0000
Content-Type: text/plain; charset="UTF-8"
 by: AdaMagica - Wed, 21 Apr 2021 17:26 UTC

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.
Why do you need this?

Re: How to check class type ?

<608061f1$0$21614$426a74cc@news.free.fr>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=5096&group=comp.lang.ada#5096

  copy link   Newsgroups: comp.lang.ada
Path: i2pn2.org!i2pn.org!news.niel.me!news.gegeweb.eu!gegeweb.org!usenet-fr.net!proxad.net!feeder1-2.proxad.net!cleanfeed1-a.proxad.net!nnrp1-1.free.fr!not-for-mail
Subject: Re: How to check class type ?
Newsgroups: comp.lang.ada
References: <60805c0e$0$12690$426a74cc@news.free.fr>
<d66d1eb4-f642-4962-9674-0e70ac2ca537n@googlegroups.com>
From: 314...@drpi.fr (DrPi)
Date: Wed, 21 Apr 2021 19:33:30 +0200
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:78.0) Gecko/20100101
Thunderbird/78.9.1
MIME-Version: 1.0
In-Reply-To: <d66d1eb4-f642-4962-9674-0e70ac2ca537n@googlegroups.com>
Content-Type: text/plain; charset=utf-8; format=flowed
Content-Language: en-US
Content-Transfer-Encoding: 8bit
Lines: 21
Message-ID: <608061f1$0$21614$426a74cc@news.free.fr>
Organization: Guest of ProXad - France
NNTP-Posting-Date: 21 Apr 2021 19:33:37 CEST
NNTP-Posting-Host: 82.65.30.55
X-Trace: 1619026417 news-3.free.fr 21614 82.65.30.55:52533
X-Complaints-To: abuse@proxad.net
 by: DrPi - Wed, 21 Apr 2021 17:33 UTC

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.
> Why do you need this?
>
I have a Vector which contains pointers to Leaf1/Leaf2 instances.
At some point, I need to know what the cursor points to.

Re: How to check class type ?

<s5po26$5fk$1@gioia.aioe.org>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=5097&group=comp.lang.ada#5097

  copy link   Newsgroups: comp.lang.ada
Path: i2pn2.org!i2pn.org!aioe.org!5WHqCw2XxjHb2npjM9GYbw.user.gioia.aioe.org.POSTED!not-for-mail
From: mail...@dmitry-kazakov.de (Dmitry A. Kazakov)
Newsgroups: comp.lang.ada
Subject: Re: How to check class type ?
Date: Wed, 21 Apr 2021 19:40:25 +0200
Organization: Aioe.org NNTP Server
Lines: 66
Message-ID: <s5po26$5fk$1@gioia.aioe.org>
References: <60805c0e$0$12690$426a74cc@news.free.fr>
NNTP-Posting-Host: 5WHqCw2XxjHb2npjM9GYbw.user.gioia.aioe.org
Mime-Version: 1.0
Content-Type: text/plain; charset=utf-8; format=flowed
Content-Transfer-Encoding: 8bit
X-Complaints-To: abuse@aioe.org
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:78.0) Gecko/20100101
Thunderbird/78.9.1
X-Notice: Filtered by postfilter v. 0.9.2
Content-Language: en-US
 by: Dmitry A. Kazakov - Wed, 21 Apr 2021 17:40 UTC

On 2021-04-21 19:08, DrPi wrote:
> Hi,
>
> I have the following declarations (just for the example) :
>
> type Root is tagged null record;
> type Root_Ptr is access all Root'Class;
>
> type Leaf1 is new Root with null record;
> type Leaf1_Ptr is access all Leaf1'Class;
>
> type Leaf2 is new Root with null record;
> type Leaf2_Ptr is access all Leaf2'Class;
>
>
> To check if a variable is of type Leaf1_Ptr or Leaf2_Ptr, I do :

That does not make sense, Leaf1_Ptr is not tagged.

> if var'Tag = Leaf1'Tag then
>   ...
> elsif var'Tag = Leaf2'Tag then
>   ...
> end if;

if Var in Leaf1'Class then
... -- Class rooted in Leaf1
elsif Var in Leaf2'Class then
... -- Class rooted in Leaf2

> It works. But is this correct ? Should the test be done differently to
> suit Ada coding style ?

It depends on the intent. Usually you should never check for the class
except two cases:

1. Defensive programming of a downcast:

if X in T'Class then
declare
Y : T'Class renames T'Class (X);
begin
...
end;

Downcasts are by themselves problematic, to be avoided when possible.

2. Emulation of multiple dispatch.

In most cases you leave all tag checks to dispatch.

This may lead to so-called God-classes when primitive operations migrate
to the root. E.g. if you had a primitive Foo on Leaf1, but not in Leaf2
and the purpose of check would be if you could call to Foo, you might
move Foo as abstract to Root and override it as raising exception in
Leaf2. Then instead of checking for the tag, you would simply dispatch:

Var.Foo; -- Will raise if not Leaf1

This would be a God-class case.

--
Regards,
Dmitry A. Kazakov
http://www.dmitry-kazakov.de

Re: How to check class type ?

<6080656d$0$3708$426a34cc@news.free.fr>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=5098&group=comp.lang.ada#5098

  copy link   Newsgroups: comp.lang.ada
Path: i2pn2.org!i2pn.org!news.niel.me!news.gegeweb.eu!gegeweb.org!usenet-fr.net!proxad.net!feeder1-2.proxad.net!cleanfeed2-a.proxad.net!nnrp1-1.free.fr!not-for-mail
Newsgroups: comp.lang.ada
References: <60805c0e$0$12690$426a74cc@news.free.fr>
<d66d1eb4-f642-4962-9674-0e70ac2ca537n@googlegroups.com>
From: 314...@drpi.fr (DrPi)
Subject: Re: How to check class type ?
Date: Wed, 21 Apr 2021 19:48:22 +0200
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:78.0) Gecko/20100101
Thunderbird/78.9.1
MIME-Version: 1.0
In-Reply-To: <d66d1eb4-f642-4962-9674-0e70ac2ca537n@googlegroups.com>
Content-Type: text/plain; charset=utf-8; format=flowed
Content-Language: en-US
Content-Transfer-Encoding: 8bit
Lines: 26
Message-ID: <6080656d$0$3708$426a34cc@news.free.fr>
Organization: Guest of ProXad - France
NNTP-Posting-Date: 21 Apr 2021 19:48:29 CEST
NNTP-Posting-Host: 82.65.30.55
X-Trace: 1619027309 news-4.free.fr 3708 82.65.30.55:52584
X-Complaints-To: abuse@proxad.net
 by: DrPi - Wed, 21 Apr 2021 17:48 UTC

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.
Thanks

> Why do you need this?
>

Re: How to check class type ?

<608138ce$0$32491$426a74cc@news.free.fr>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=5100&group=comp.lang.ada#5100

  copy link   Newsgroups: comp.lang.ada
Path: i2pn2.org!i2pn.org!weretis.net!feeder8.news.weretis.net!proxad.net!feeder1-2.proxad.net!cleanfeed3-a.proxad.net!nnrp1-1.free.fr!not-for-mail
Newsgroups: comp.lang.ada
References: <60805c0e$0$12690$426a74cc@news.free.fr>
<s5po26$5fk$1@gioia.aioe.org>
From: 314...@drpi.fr (DrPi)
Subject: Re: How to check class type ?
Date: Thu, 22 Apr 2021 10:50:22 +0200
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:78.0) Gecko/20100101
Thunderbird/78.9.1
MIME-Version: 1.0
In-Reply-To: <s5po26$5fk$1@gioia.aioe.org>
Content-Type: text/plain; charset=utf-8; format=flowed
Content-Language: en-US
Content-Transfer-Encoding: 8bit
Lines: 65
Message-ID: <608138ce$0$32491$426a74cc@news.free.fr>
Organization: Guest of ProXad - France
NNTP-Posting-Date: 22 Apr 2021 10:50:22 CEST
NNTP-Posting-Host: 82.65.30.55
X-Trace: 1619081422 news-2.free.fr 32491 82.65.30.55:57973
X-Complaints-To: abuse@proxad.net
 by: DrPi - Thu, 22 Apr 2021 08:50 UTC

Le 21/04/2021 à 19:40, Dmitry A. Kazakov a écrit :
> On 2021-04-21 19:08, DrPi wrote:
>> Hi,
>>
>> I have the following declarations (just for the example) :
>>
>> type Root is tagged null record;
>> type Root_Ptr is access all Root'Class;
>>
>> type Leaf1 is new Root with null record;
>> type Leaf1_Ptr is access all Leaf1'Class;
>>
>> type Leaf2 is new Root with null record;
>> type Leaf2_Ptr is access all Leaf2'Class;
>>
>>
>> To check if a variable is of type Leaf1_Ptr or Leaf2_Ptr, I do :
>
> That does not make sense, Leaf1_Ptr is not tagged.
>
>> if var'Tag = Leaf1'Tag then
>>    ...
>> elsif var'Tag = Leaf2'Tag then
>>    ...
>> end if;
>
>
>   if Var in Leaf1'Class then
>      ... -- Class rooted in Leaf1
>   elsif Var in Leaf2'Class then
>      ... -- Class rooted in Leaf2
>
>> It works. But is this correct ? Should the test be done differently to
>> suit Ada coding style ?
>
> It depends on the intent. Usually you should never check for the class
> except two cases:
>
> 1. Defensive programming of a downcast:
>
>   if X in T'Class then
>      declare
>         Y : T'Class renames T'Class (X);
>      begin
>         ...
>      end;
>
> Downcasts are by themselves problematic, to be avoided when possible.
>
> 2. Emulation of multiple dispatch.
>
> In most cases you leave all tag checks to dispatch.
>
> This may lead to so-called God-classes when primitive operations migrate
> to the root. E.g. if you had a primitive Foo on Leaf1, but not in Leaf2
> and the purpose of check would be if you could call to Foo, you might
> move Foo as abstract to Root and override it as raising exception in
> Leaf2. Then instead of checking for the tag, you would simply dispatch:
>
>   Var.Foo; -- Will raise if not Leaf1
>
> This would be a God-class case.
>
Thanks for the explanations. As always, very instructive.

Re: How to check class type ?

<s5urno$m4d$1@dont-email.me>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=5107&group=comp.lang.ada#5107

  copy link   Newsgroups: comp.lang.ada
Path: i2pn2.org!i2pn.org!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: joh...@nospam.mccabe.org.uk (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
Lines: 42
Message-ID: <s5urno$m4d$1@dont-email.me>
References: <60805c0e$0$12690$426a74cc@news.free.fr>
<d66d1eb4-f642-4962-9674-0e70ac2ca537n@googlegroups.com>
<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=
 by: John McCabe - Fri, 23 Apr 2021 16:13 UTC

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

1
server_pubkey.txt

rocksolid light 0.9.8
clearnet tor