digitalmars.D.learn - Why isn't == used to compare structs
- Jacob Carlborg <doob me.com> Feb 08 2010
- Manuel =?ISO-2022-JP?B?SxskKEQrUxsoQm5pZw==?= <manuelk89 gmx.net> Feb 08 2010
- Trass3r <un known.com> Feb 08 2010
- =?UTF-8?B?UGVsbGUgTcOlbnNzb24=?= <pelle.mansson gmail.com> Feb 08 2010
- Jacob Carlborg <doob me.com> Feb 08 2010
- Jacob Carlborg <doob me.com> Feb 08 2010
- Don <nospam nospam.com> Feb 09 2010
- grauzone <none example.net> Feb 09 2010
- Don <nospam nospam.com> Feb 09 2010
- Trass3r <un known.com> Feb 08 2010
- Trass3r <un known.com> Feb 08 2010
- Trass3r <un known.com> Feb 09 2010
- "Steven Schveighoffer" <schveiguy yahoo.com> Feb 09 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.
Feb 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.
Feb 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.
Feb 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.
Feb 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.
Feb 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 ==.
Feb 08 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 ==.
Feb 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 ==.
Feb 09 2010
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
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
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
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
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*!
==.
Seems arrays inside structs still are not compared with ==.
http://d.puremagic.com/issues/show_bug.cgi?id=3789
Feb 09 2010









Manuel =?ISO-2022-JP?B?SxskKEQrUxsoQm5pZw==?= <manuelk89 gmx.net> 