www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - opIs broke for structs?

reply "Era Scarecrow" <rtcvb32 yahoo.com> writes:
  I'm curious why this would break and not work, as is should 
compare against it's location/entity correct? I wonder why it 
breaks then for structs on the stack. I can only find 'opIs 
missing' when searching for it, so...

struct X {
   int x;
}

class Y {
   int y;
}

   Y y1 = new Y();
   Y y2 = new Y();
   X x1, x2;

   assert(y1.y == y2.y);
   assert(y1 !is y2);
   assert(y1 != y2); //same as !is by default as i recall

//  writefln("x1 = 0x%s\nx2 = 0x%s", &x1, &x2);
   assert(x1 == x2);
   assert(x1 !is x2); //fails
Oct 13 2012
next sibling parent "H. S. Teoh" <hsteoh quickfur.ath.cx> writes:
On Sat, Oct 13, 2012 at 11:40:04PM +0200, Era Scarecrow wrote:
  I'm curious why this would break and not work, as is should compare
 against it's location/entity correct? I wonder why it breaks then
 for structs on the stack. I can only find 'opIs missing' when
 searching for it, so...
[...] Wait, there's such a thing as overloading 'is'? If there is, that's pretty messed up. The 'is' operator is used for a lot of fundamental stuff, and allowing structs and classes to change that just sounds ... wrong. T -- It's amazing how careful choice of punctuation can leave you hanging:
Oct 13 2012
prev sibling parent reply "bearophile" <bearophileHUGS lycos.com> writes:
Era Scarecrow:

  I'm curious why this would break and not work, as is should 
 compare against it's location/entity correct? I wonder why it 
 breaks then for structs on the stack. I can only find 'opIs 
 missing' when searching for it, so...
"is" is meant to perform a bitwise comparison (I think today it is working as designed or it's very close to this target). So on class references "is" performs a bitwise comparison of the references themselves. On values (like structs) "is" compares the struct values bit-wise. In your code x1 and x2 contain the same bit patterns (32 bits set to zero), so "x1 is x2" is true. Bye, bearophile
Oct 13 2012
parent "Era Scarecrow" <rtcvb32 yahoo.com> writes:
On Saturday, 13 October 2012 at 22:19:44 UTC, H. S. Teoh wrote:
 Wait, there's such a thing as overloading 'is'? If there is, 
 that's pretty messed up. The 'is' operator is used for a lot of 
 fundamental stuff, and allowing structs and classes to change 
 that just sounds ... wrong.
Sure there is, it's just not one you can override to my knowledge (along with in (AA & array) I believe). I only referred to it as opIs since that's likely how internally the compiler deals with it, plus there's a reference to opIs in 2008. On Saturday, 13 October 2012 at 22:42:35 UTC, bearophile wrote:
 "is" is meant to perform a bitwise comparison (I think today it 
 is working as designed or it's very close to this target).
 So on class references "is" performs a bitwise comparison of 
 the references themselves.
 On values (like structs) "is" compares the struct values 
 bit-wise.

 In your code x1 and x2 contain the same bit patterns (32 bits 
 set to zero), so "x1 is x2" is true.
TDPL pg. 57 [quote] The expression a is b compares for alias equality and returns true if a and be refer to the same actual object. * If a and b are arrays or class references, the result is true if and only if a and b are two named for the same actual object. * Otherwise, a is b is the same as a == b [/quote] Hmm I had more the impression it was address space only. But if it's in the manual and spec, then I'm wrong and just needed a refresher on it. Course if structs are relocatable and are identical, they are as good as the same object. So *shrugs* Sorry for bringing it up then.
Oct 13 2012