www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Asserting that a reference is valid

reply Brad Beveridge <brad.beveridge somewhere.com> writes:
Hi all, this sounds like a daft question, but I can't find in the docs where
it is addressed.
The following code on linux with dmd0.94 segfaults.
Object o;
assert (o != null);

Is there another way that I can assert that o is a valid reference before I
use it?

Cheers
Brad
Jul 26 2004
next sibling parent reply Derek Parnell <derek psych.ward> writes:
On Mon, 26 Jul 2004 19:41:19 +1200, Brad Beveridge wrote:

 Hi all, this sounds like a daft question, but I can't find in the docs where
 it is addressed.
 The following code on linux with dmd0.94 segfaults.
 Object o;
 assert (o != null);
 
 Is there another way that I can assert that o is a valid reference before I
 use it?
 
 Cheers
 Brad

Try assert (o !== null) or assert (!(o === null)); or assert (!(o is null)); -- Derek Melbourne, Australia 26/Jul/04 5:43:18 PM
Jul 26 2004
parent Brad Beveridge <brad.beveridge somewhere.com> writes:
Thank you, now that I see the === operator it rings a bell :)  It has been a
long time since I read the D specs in fine detail.

Cheers
Brad

Derek Parnell wrote:

 On Mon, 26 Jul 2004 19:41:19 +1200, Brad Beveridge wrote:
 
 Hi all, this sounds like a daft question, but I can't find in the docs
 where it is addressed.
 The following code on linux with dmd0.94 segfaults.
 Object o;
 assert (o != null);
 
 Is there another way that I can assert that o is a valid reference before
 I use it?
 
 Cheers
 Brad

Try assert (o !== null) or assert (!(o === null)); or assert (!(o is null));

Jul 26 2004
prev sibling parent reply Ilya Minkov <minkov cs.tum.edu> writes:
Brad Beveridge schrieb:

 Is there another way that I can assert that o is a valid reference before I
 use it?

assert(o); -eye
Jul 26 2004
parent reply "Vathix" <vathixSpamFix dprogramming.com> writes:
"Ilya Minkov" <minkov cs.tum.edu> wrote in message
news:ce2qo1$quf$3 digitaldaemon.com...
 Brad Beveridge schrieb:

 Is there another way that I can assert that o is a valid reference


 use it?

assert(o); -eye

That's a special case that calls the invariant on the object. If it's null, you'll get an access violation. I wish a simple o!==null would be automatically tested before the invariant, but oh well.
Jul 26 2004
parent "C. Sauls" <ibisbasenji yahoo.com> writes:
Vathix wrote:
 "Ilya Minkov" <minkov cs.tum.edu> wrote in message
assert(o);

That's a special case that calls the invariant on the object. If it's null, you'll get an access violation. I wish a simple o!==null would be automatically tested before the invariant, but oh well.

You could always do: assert(o !== null && o); But that's kind of... well, ugly, to me. -Chris S. -Invironz
Jul 26 2004