Thread overview
Errm so what does "if (object)" and "Assert(object)" do??
Mar 10, 2007
Chris Warwick
Mar 10, 2007
Hasan Aljudy
Mar 10, 2007
torhu
Mar 10, 2007
Derek Parnell
Mar 10, 2007
Max Samukha
March 10, 2007
Errm so what does "if (object)" and "Assert(object)" do??

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

cheers,

cw


March 10, 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)".


March 10, 2007

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
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
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
"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
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