"==" not symmetric

Unknown W. Brackets unknown at simplemachines.org
Tue May 23 21:02:05 PDT 2006


Forgive my rusty logic skills... how do I read your table?  As far as I 
see it:

o1     o2     result
null   null   true
null   valid  false
null   bad    false
valid  null   result of opEquals(null) - probably false
valid  valid  result of opEquals(valid) - unknown result
valid  bad    result of opEquals(bad) - probably false
bad    null   segfault
bad    valid  segfault
bad    bad    segfault unless equal bad pointers, then true

I don't know what "y" is on your table.

Of course, you could throw that off with an opEquals like this:

bool opEquals(Object o)
{
    return true;
}

Obviously, we have to assume that o1 and o2 are of the same type, or 
else different opEquals might be called.  So, okay... the only 
differences we have:

If o1 is null, reversing the comparison will depend on opEquals 
returning false when it is passed a null pointer (which should be the case.)

Comparing a valid pointer to a bad pointer cannot be properly reversed, 
or it might cause a segfault.  This is always wrong anyway imho.

That said, opEquals (imho) will either always check for null or cannot 
be reversed (null == evaluatesToNullObject is always false.)  So it 
would seem logical to do something like you suggested.

-[Unknown]


> truth table:
> o1 o2   result
>  0   0     1
>  0   x     0
>  x   0     opEquals, crash?
>  x   x     1
>  y   x     opEquals
> 
> It's interesting that (o1==o2) is not necessarily the same as (o2==o1)!
> Since o1=x,o2=0 returns 0, the other way around should return 0 as well.
> 
> I suppose the function should be also check o2, resulting in:
> 
> #int _d_obj_eq(Object o1, Object o2)
> #{
> #    return o1 is o2 || (o1 && o2 && o1.opEquals(o2));
> #}



More information about the Digitalmars-d mailing list