Thread overview
Asserting that a reference is valid
Jul 26, 2004
Brad Beveridge
Jul 26, 2004
Derek Parnell
Jul 26, 2004
Brad Beveridge
Jul 26, 2004
Ilya Minkov
Jul 26, 2004
Vathix
Jul 26, 2004
C. Sauls
July 26, 2004
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
July 26, 2004
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
July 26, 2004
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));
> 
> 

July 26, 2004
Brad Beveridge schrieb:

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

assert(o);

-eye
July 26, 2004
"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
before I
> > 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.


July 26, 2004
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