Thread overview | ||||||||
---|---|---|---|---|---|---|---|---|
|
July 26, 2004 Asserting that a reference is valid | ||||
---|---|---|---|---|
| ||||
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 Re: Asserting that a reference is valid | ||||
---|---|---|---|---|
| ||||
Posted in reply to Brad Beveridge | 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 Re: Asserting that a reference is valid | ||||
---|---|---|---|---|
| ||||
Posted in reply to Derek Parnell | 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 Re: Asserting that a reference is valid | ||||
---|---|---|---|---|
| ||||
Posted in reply to Brad Beveridge | 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 Re: Asserting that a reference is valid | ||||
---|---|---|---|---|
| ||||
Posted in reply to Ilya Minkov | "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 Re: Asserting that a reference is valid | ||||
---|---|---|---|---|
| ||||
Posted in reply to Vathix | 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
|
Copyright © 1999-2021 by the D Language Foundation