www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Why isn't == used to compare structs

reply Jacob Carlborg <doob me.com> writes:
struct String
{
	char[] data;
}

void main ()
{
	auto foo = String("foo");
	auto bar = String("foo".dup);

	assert(bar == foo);
}

Why isn't == used to compare the struct members in the code above? I 
mean, if I compare the structs with == it could also use == to compare 
the members. If I use "is" to compare the structs it could use "is" to 
compare them members.
Feb 08 2010
next sibling parent Manuel =?ISO-2022-JP?B?SxskKEQrUxsoQm5pZw==?= <manuelk89 gmx.net> writes:
Am Mon, 08 Feb 2010 12:19:12 +0100
schrieb Jacob Carlborg <doob me.com>:

 struct String
 {
 	char[] data;
 }
 
 void main ()
 {
 	auto foo = String("foo");
 	auto bar = String("foo".dup);
 
 	assert(bar == foo);
 }
 
 Why isn't == used to compare the struct members in the code above? I 
 mean, if I compare the structs with == it could also use == to
 compare the members. If I use "is" to compare the structs it could
 use "is" to compare them members.
How should == be used for this struct? struct Foo { union { char[] dataA; long dataB; } } Foo a,b; a.dataA = "abc"; b.dataA = "abc".dup; Now a.dataA == b.dataA, but a.dataB != b.dataB.
Feb 08 2010
prev sibling parent reply Trass3r <un known.com> writes:
 Why isn't == used to compare the struct members in the code above? I  
 mean, if I compare the structs with == it could also use == to compare  
 the members. If I use "is" to compare the structs it could use "is" to  
 compare them members.
Structs are compared *bitwise*! When you dup your pointer is different and thus the structs are different.
Feb 08 2010
next sibling parent reply =?UTF-8?B?UGVsbGUgTcOlbnNzb24=?= <pelle.mansson gmail.com> writes:
On 02/08/2010 01:48 PM, Trass3r wrote:
 Why isn't == used to compare the struct members in the code above? I
 mean, if I compare the structs with == it could also use == to compare
 the members. If I use "is" to compare the structs it could use "is" to
 compare them members.
Structs are compared *bitwise*! When you dup your pointer is different and thus the structs are different.
I believe the question was *why* things are this way. I think it's weird.
Feb 08 2010
next sibling parent Trass3r <un known.com> writes:
 I believe the question was *why* things are this way. I think it's weird.
It's common behavior. In the end structs are just a way to map a memory range to some variable tuple. If you really need == for all members you can always overload opEquals!!
Feb 08 2010
prev sibling parent Jacob Carlborg <doob me.com> writes:
On 2/8/10 14:58, Pelle MÃ¥nsson wrote:
 On 02/08/2010 01:48 PM, Trass3r wrote:
 Why isn't == used to compare the struct members in the code above? I
 mean, if I compare the structs with == it could also use == to compare
 the members. If I use "is" to compare the structs it could use "is" to
 compare them members.
Structs are compared *bitwise*! When you dup your pointer is different and thus the structs are different.
I believe the question was *why* things are this way. I think it's weird.
Yes that was the question.
Feb 08 2010
prev sibling next sibling parent reply Jacob Carlborg <doob me.com> writes:
On 2/8/10 13:48, Trass3r wrote:
 Why isn't == used to compare the struct members in the code above? I
 mean, if I compare the structs with == it could also use == to compare
 the members. If I use "is" to compare the structs it could use "is" to
 compare them members.
Structs are compared *bitwise*! When you dup your pointer is different and thus the structs are different.
Yes, I intentionally duped the string to make sure they're not using the same memory. I was thinking of something like a transitive ==.
Feb 08 2010
parent Trass3r <un known.com> writes:
 Yes, I intentionally duped the string to make sure they're not using the  
 same memory. I was thinking of something like a transitive ==.
Overload opEquals.
Feb 08 2010
prev sibling parent reply Don <nospam nospam.com> writes:
Trass3r wrote:
 Why isn't == used to compare the struct members in the code above? I 
 mean, if I compare the structs with == it could also use == to compare 
 the members. If I use "is" to compare the structs it could use "is" to 
 compare them members.
Structs are compared *bitwise*!
Not in D2, any more. If a member has an opEquals, it's compared using ==.
Feb 09 2010
next sibling parent reply grauzone <none example.net> writes:
Don wrote:
 Trass3r wrote:
 Why isn't == used to compare the struct members in the code above? I 
 mean, if I compare the structs with == it could also use == to 
 compare the members. If I use "is" to compare the structs it could 
 use "is" to compare them members.
Structs are compared *bitwise*!
Not in D2, any more. If a member has an opEquals, it's compared using ==.
Seems arrays inside structs still are not compared with ==.
Feb 09 2010
parent "Steven Schveighoffer" <schveiguy yahoo.com> writes:
On Tue, 09 Feb 2010 14:58:13 -0500, grauzone <none example.net> wrote:

 Don wrote:
 Trass3r wrote:
 Why isn't == used to compare the struct members in the code above? I  
 mean, if I compare the structs with == it could also use == to  
 compare the members. If I use "is" to compare the structs it could  
 use "is" to compare them members.
Structs are compared *bitwise*!
Not in D2, any more. If a member has an opEquals, it's compared using ==.
Seems arrays inside structs still are not compared with ==.
http://d.puremagic.com/issues/show_bug.cgi?id=3789
Feb 09 2010
prev sibling parent reply Trass3r <un known.com> writes:
  Structs are compared *bitwise*!
Not in D2, any more. If a member has an opEquals, it's compared using ==.
Ok, what's the rationale?
Feb 09 2010
parent Don <nospam nospam.com> writes:
Trass3r wrote:
  Structs are compared *bitwise*!
Not in D2, any more. If a member has an opEquals, it's compared using ==.
Ok, what's the rationale?
If there is an opEquals, it should not be ignored. If it has no opEquals, it's just considered to be a bag of bits.
Feb 09 2010