Re: Why isn't == used to compare structs

2010-02-09 Thread Don

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 ==.


Re: Why isn't == used to compare structs

2010-02-09 Thread grauzone

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 ==.


Re: Why isn't == used to compare structs

2010-02-09 Thread Trass3r

 Structs are compared *bitwise*!


Not in D2, any more. If a member has an opEquals, it's compared using ==.


Ok, what's the rationale?


Re: Why isn't == used to compare structs

2010-02-09 Thread Steven Schveighoffer

On Tue, 09 Feb 2010 14:58:13 -0500, grauzone n...@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


Re: Why isn't == used to compare structs

2010-02-09 Thread Don

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.


Why isn't == used to compare structs

2010-02-08 Thread Jacob Carlborg

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.


Re: Why isn't == used to compare structs

2010-02-08 Thread Manuel König
Am Mon, 08 Feb 2010 12:19:12 +0100
schrieb Jacob Carlborg d...@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.


Re: Why isn't == used to compare structs

2010-02-08 Thread Trass3r
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.


Re: Why isn't == used to compare structs

2010-02-08 Thread Pelle Månsson

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.


Re: Why isn't == used to compare structs

2010-02-08 Thread Trass3r

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


Re: Why isn't == used to compare structs

2010-02-08 Thread Jacob Carlborg

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.


Re: Why isn't == used to compare structs

2010-02-08 Thread Jacob Carlborg

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 ==.


Re: Why isn't == used to compare structs

2010-02-08 Thread Trass3r
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.