digitalmars.D.learn - Errm so what does "if (object)" and "Assert(object)" do??
- Chris Warwick (4/4) Mar 09 2007 Errm so what does "if (object)" and "Assert(object)" do??
- Jarrett Billingsley (21/22) Mar 09 2007 "if(object)" is the same as "if(object !is null)".
- Hasan Aljudy (4/34) Mar 09 2007 Really?
- torhu (16/33) Mar 10 2007 It's mentioned here:
- Derek Parnell (7/12) Mar 10 2007 Yes. That would be a very good idea.
- Jarrett Billingsley (3/6) Mar 10 2007 votes = votes + 1; // add one to votes
- Max Samukha (3/9) Mar 10 2007 vote++ for obj.invariant
Errm so what does "if (object)" and "Assert(object)" do?? Are they the same as (object !is null)? cheers, cw
Mar 09 2007
"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
Jarrett Billingsley wrote:"Chris Warwick" <sp m.me.not> wrote in message news:est5n5$1krh$1 digitalmars.com...Really? I thought assert(object) just throws an assert error if the reference is null!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
Hasan Aljudy wrote:Jarrett Billingsley wrote: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?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 10 2007
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
"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
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...vote++ for obj.invariantI 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