Thread overview | |||||||||
---|---|---|---|---|---|---|---|---|---|
|
March 10, 2007 Errm so what does "if (object)" and "Assert(object)" do?? | ||||
---|---|---|---|---|
| ||||
Errm so what does "if (object)" and "Assert(object)" do?? Are they the same as (object !is null)? cheers, cw |
March 10, 2007 Re: Errm so what does "if (object)" and "Assert(object)" do?? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Chris Warwick | "Chris Warwick" <sp@m.me.not> wrote in message news:est5n5$1krh$1@digitalmars.com... > Errm so what does "if (object)" and "Assert(object)" do?? "if(object)" is the same as "if(object !is null)". But "assert(object)" is a little tricky. It's a little-known feature. If you use "assert(object)", it will call any invariants defined for the object, i.e. class C { invariant { writefln("foo"); } } ... scope c = new C(); assert(c); // prints foo since it calls the invariant This means that if you use "assert(objectReference)" on a null reference, you will (like with '==' !) get an access violation, since it tries to look up the invariant from a null reference. Therefore, when doing an assert on an object reference, you should always use "assert(c is null)" or "assert(c !is null)". |
March 10, 2007 Re: Errm so what does "if (object)" and "Assert(object)" do?? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Jarrett Billingsley |
Jarrett Billingsley wrote:
> "Chris Warwick" <sp@m.me.not> wrote in message news:est5n5$1krh$1@digitalmars.com...
>> Errm so what does "if (object)" and "Assert(object)" do??
>
> "if(object)" is the same as "if(object !is null)".
>
> But "assert(object)" is a little tricky. It's a little-known feature. If you use "assert(object)", it will call any invariants defined for the object, i.e.
>
> class C
> {
> invariant
> {
> writefln("foo");
> }
> }
>
> ...
> scope c = new C();
> assert(c); // prints foo since it calls the invariant
>
> This means that if you use "assert(objectReference)" on a null reference, you will (like with '==' !) get an access violation, since it tries to look up the invariant from a null reference.
>
> Therefore, when doing an assert on an object reference, you should always use "assert(c is null)" or "assert(c !is null)".
>
>
Really?
I thought assert(object) just throws an assert error if the reference is null!
|
March 10, 2007 Re: Errm so what does "if (object)" and "Assert(object)" do?? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Hasan Aljudy | Hasan Aljudy wrote: > > Jarrett Billingsley wrote: >> scope c = new C(); >> assert(c); // prints foo since it calls the invariant >> >> This means that if you use "assert(objectReference)" on a null reference, you will (like with '==' !) get an access violation, since it tries to look up the invariant from a null reference. >> >> Therefore, when doing an assert on an object reference, you should always use "assert(c is null)" or "assert(c !is null)". >> >> > > Really? > I thought assert(object) just throws an assert error if the reference is null! It's mentioned here: http://www.digitalmars.com/d/class.html#invariants The funny part is that assert(obj !is null && obj) does *not* call the invariant. So assert(obj) is a special case. I think assert(obj) being the same as assert(obj !is null), and then being able to do 'obj.invariant' if you really want to explicitly check the invariant would a lot clearer. Or even 'invariant(obj)'. Calling the invariant explicitly might be useful in the middle of methods. Then I think this.invariant or invariant(this), is a lot more readable than assert(this). You want both a null check and an invariant check, you could then do 'assert(obj && invariant(obj))' or assert(obj && obj.invariant)'. Seems pretty obvious to me what that would do. Maybe obj.invariant is the best way, since it doesn't try to hide that invariant really is a method? |
March 10, 2007 Re: Errm so what does "if (object)" and "Assert(object)" do?? | ||||
---|---|---|---|---|
| ||||
Posted in reply to torhu | On Sat, 10 Mar 2007 11:05:34 +0100, torhu wrote: > So assert(obj) is a special case. > > I think assert(obj) being the same as assert(obj !is null), and then being able to do 'obj.invariant' if you really want to explicitly check the invariant would a lot clearer. Or even 'invariant(obj)'. Yes. That would be a very good idea. -- Derek Parnell Melbourne, Australia "Justice for David Hicks!" skype: derek.j.parnell |
March 10, 2007 Re: Errm so what does "if (object)" and "Assert(object)" do?? | ||||
---|---|---|---|---|
| ||||
Posted in reply to torhu | "torhu" <fake@address.dude> wrote in message news:estvsu$2pd9$1@digitalmars.com... > I think assert(obj) being the same as assert(obj !is null), and then being able to do 'obj.invariant' if you really want to explicitly check the invariant would a lot clearer. Or even 'invariant(obj)'. votes = votes + 1; // add one to votes |
March 10, 2007 Re: Errm so what does "if (object)" and "Assert(object)" do?? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Jarrett Billingsley | On Sat, 10 Mar 2007 12:43:22 -0500, "Jarrett Billingsley" <kb3ctd2@yahoo.com> wrote:
>"torhu" <fake@address.dude> wrote in message news:estvsu$2pd9$1@digitalmars.com...
>> I think assert(obj) being the same as assert(obj !is null), and then being able to do 'obj.invariant' if you really want to explicitly check the invariant would a lot clearer. Or even 'invariant(obj)'.
>
>votes = votes + 1; // add one to votes
>
vote++ for obj.invariant
|
Copyright © 1999-2021 by the D Language Foundation