comp.lang.ada
 help / color / mirror / Atom feed
From: "Randy Brukardt" <randy@rrsoftware.com>
Subject: Re: How best to test for NULL in Ada?
Date: Thu, 2 Jul 2015 19:12:51 -0500
Date: 2015-07-02T19:12:51-05:00	[thread overview]
Message-ID: <mn4k24$ndj$1@loke.gir.dk> (raw)
In-Reply-To: mn4gr7$97r$2@dont-email.me

"Jeffrey R. Carter" <spam.jrcarter.not@spam.not.acm.org> wrote in message 
news:mn4gr7$97r$2@dont-email.me...
> On 07/02/2015 03:15 PM, NiGHTS wrote:
>> I have encountered several situations where I needed to test an access 
>> type
>> for NULL or not. My instinct is to do something like this:
>>
>> if Some_Object /= null then ...
>
> That is correct.
>
>> But many times I get this error:
>>
>> invalid operand type for operator "/=" left operand has type
>> "Some_Object_Type" defined at ... right operand has an access type
>
> This says that Some_Object is not a value of an access type, so you can't
> compare it to null.

Right. You often get an error that the operator is not visible, which means 
that a suitable "use type" clause is needed.

In Ada 2012 there is a hack available to avoid the use clause. (This comes 
from Tucker Taft, who admitted to doing this during our recent ARG meeting 
in Madrid. He got suitable groans for this usage...)

    if Some_Object not in null then

This membership uses the predefined equality for the type of Some_Object, 
and it need not be visible. But this is a hack (it harms readability), and 
it's dangerous if "=" has been redefined (as it always uses the predefined 
"=", not the overridden one). Use at your own risk.

                                      Randy. 



  reply	other threads:[~2015-07-03  0:12 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-07-02 22:15 How best to test for NULL in Ada? NiGHTS
2015-07-02 23:19 ` Jeffrey R. Carter
2015-07-03  0:12   ` Randy Brukardt [this message]
2015-07-04  2:02     ` Shark8
2015-07-03  7:30   ` Dmitry A. Kazakov
2015-07-05 23:32 ` NiGHTS
replies disabled

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