www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Errm so what does "if (object)" and "Assert(object)" do??

reply "Chris Warwick" <sp m.me.not> writes:
Errm so what does "if (object)" and "Assert(object)" do??

Are they the same as (object !is null)?

cheers,

cw 
Mar 09 2007
parent reply "Jarrett Billingsley" <kb3ctd2 yahoo.com> writes:
"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)".
Mar 09 2007
parent reply Hasan Aljudy <hasan.aljudy gmail.com> writes:
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!
Mar 09 2007
parent reply torhu <fake address.dude> writes:
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?
Mar 10 2007
next sibling parent Derek Parnell <derek psych.ward> writes:
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
Mar 10 2007
prev sibling parent reply "Jarrett Billingsley" <kb3ctd2 yahoo.com> writes:
"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
Mar 10 2007
parent Max Samukha <samukha voliacable.com> writes:
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
Mar 10 2007