comp.lang.ada
 help / color / mirror / Atom feed
* How best to test for NULL in Ada?
@ 2015-07-02 22:15 NiGHTS
  2015-07-02 23:19 ` Jeffrey R. Carter
  2015-07-05 23:32 ` NiGHTS
  0 siblings, 2 replies; 6+ messages in thread
From: NiGHTS @ 2015-07-02 22:15 UTC (permalink / raw)


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

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

What is the correct way to test for NULL in Ada?


(If you want a specific example, I am trying to test if the Selected Path returned by Gtk.Tree_View.Get_Cursor in the GTK Library is valid or NULL. But situations like these occur many times in distinct situations so I'm not looking for a fix to this specific problem)


Thank you in advance for your help!

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: How best to test for NULL in Ada?
  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
  2015-07-03  7:30   ` Dmitry A. Kazakov
  2015-07-05 23:32 ` NiGHTS
  1 sibling, 2 replies; 6+ messages in thread
From: Jeffrey R. Carter @ 2015-07-02 23:19 UTC (permalink / raw)


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.

> (If you want a specific example, I am trying to test if the Selected Path
> returned by Gtk.Tree_View.Get_Cursor in the GTK Library is valid or NULL. But
> situations like these occur many times in distinct situations so I'm not
> looking for a fix to this specific problem)

The path returned has type Gtk.Tree_Model.Gtk_Tree_Path, which is a tagged
record type. Since it's not an access type, you can't compare it to null. You
have to compare it to a value of type Gtk.Tree_Model.Gtk_Tree_Path.

-- 
Jeff Carter
"What lazy lout left these wires all over the lawn?"
Poppy
98

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: How best to test for NULL in Ada?
  2015-07-02 23:19 ` Jeffrey R. Carter
@ 2015-07-03  0:12   ` Randy Brukardt
  2015-07-04  2:02     ` Shark8
  2015-07-03  7:30   ` Dmitry A. Kazakov
  1 sibling, 1 reply; 6+ messages in thread
From: Randy Brukardt @ 2015-07-03  0:12 UTC (permalink / raw)


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



^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: How best to test for NULL in Ada?
  2015-07-02 23:19 ` Jeffrey R. Carter
  2015-07-03  0:12   ` Randy Brukardt
@ 2015-07-03  7:30   ` Dmitry A. Kazakov
  1 sibling, 0 replies; 6+ messages in thread
From: Dmitry A. Kazakov @ 2015-07-03  7:30 UTC (permalink / raw)


On Thu, 02 Jul 2015 16:19:22 -0700, Jeffrey R. Carter wrote:

> The path returned has type Gtk.Tree_Model.Gtk_Tree_Path, which is a tagged
> record type. Since it's not an access type, you can't compare it to null. You
> have to compare it to a value of type Gtk.Tree_Model.Gtk_Tree_Path.

Yes. There is the Null_Gtk_Tree_Path constant for that.

Gtk_Tree_Path is a wrapped pointer. One thing Ada should have is the
abstract access type to have such wrappers derived from, and thus making
comparison to null legal (instead of the implicit dereference hack).

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


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: How best to test for NULL in Ada?
  2015-07-03  0:12   ` Randy Brukardt
@ 2015-07-04  2:02     ` Shark8
  0 siblings, 0 replies; 6+ messages in thread
From: Shark8 @ 2015-07-04  2:02 UTC (permalink / raw)


On Thursday, July 2, 2015 at 6:12:53 PM UTC-6, Randy Brukardt wrote:
>
> 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.

Hm I can see where, if you were writing your own "=" operator for Some_Object's type and it was (a) an access type, and (b) you needed to test for null that could be useful.


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: How best to test for NULL in Ada?
  2015-07-02 22:15 How best to test for NULL in Ada? NiGHTS
  2015-07-02 23:19 ` Jeffrey R. Carter
@ 2015-07-05 23:32 ` NiGHTS
  1 sibling, 0 replies; 6+ messages in thread
From: NiGHTS @ 2015-07-05 23:32 UTC (permalink / raw)


This helped me out a lot. Thank you all for your help.

^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2015-07-05 23:32 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
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
2015-07-04  2:02     ` Shark8
2015-07-03  7:30   ` Dmitry A. Kazakov
2015-07-05 23:32 ` NiGHTS

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