Jump to page: 1 2
Thread overview
Why isn't == used to compare structs
Feb 08, 2010
Jacob Carlborg
Feb 08, 2010
Trass3r
Feb 08, 2010
Pelle Månsson
Feb 08, 2010
Trass3r
Feb 08, 2010
Jacob Carlborg
Feb 08, 2010
Jacob Carlborg
Feb 08, 2010
Trass3r
Feb 09, 2010
Don
Feb 09, 2010
grauzone
Feb 09, 2010
Trass3r
Feb 09, 2010
Don
February 08, 2010
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.
February 08, 2010
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.
February 08, 2010
> 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.
February 08, 2010
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.
February 08, 2010
> 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!!
February 08, 2010
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.
February 08, 2010
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 ==.
February 08, 2010
> 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.
February 09, 2010
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 ==.
February 09, 2010
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 ==.
« First   ‹ Prev
1 2