digitalmars.D.learn - Why isn't == used to compare structs
- Jacob Carlborg (14/14) Feb 08 2010 struct String
- Manuel =?ISO-2022-JP?B?SxskKEQrUxsoQm5pZw==?= (15/32) Feb 08 2010 How should == be used for this struct?
- Trass3r (2/6) Feb 08 2010 Structs are compared *bitwise*!
- =?UTF-8?B?UGVsbGUgTcOlbnNzb24=?= (2/8) Feb 08 2010 I believe the question was *why* things are this way. I think it's weird...
- Trass3r (3/4) Feb 08 2010 It's common behavior. In the end structs are just a way to map a memory ...
- Jacob Carlborg (2/12) Feb 08 2010 Yes that was the question.
- Jacob Carlborg (3/9) Feb 08 2010 Yes, I intentionally duped the string to make sure they're not using the...
- Trass3r (1/3) Feb 08 2010 Overload opEquals.
- Don (2/8) Feb 09 2010 Not in D2, any more. If a member has an opEquals, it's compared using ==...
- grauzone (2/11) Feb 09 2010 Seems arrays inside structs still are not compared with ==.
- Steven Schveighoffer (2/13) Feb 09 2010 http://d.puremagic.com/issues/show_bug.cgi?id=3789
- Trass3r (1/3) Feb 09 2010 Ok, what's the rationale?
- Don (3/8) Feb 09 2010 If there is an opEquals, it should not be ignored. If it has no
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:I believe the question was *why* things are this way. I think it's weird.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
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
On 2/8/10 14:58, Pelle MÃ¥nsson wrote:On 02/08/2010 01:48 PM, Trass3r wrote:Yes that was the question.I believe the question was *why* things are this way. I think it's weird.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 2/8/10 13:48, Trass3r wrote:Yes, I intentionally duped the string to make sure they're not using the same memory. I was thinking of something like a transitive ==.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
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
Trass3r wrote:Not in D2, any more. If a member has an opEquals, it's compared using ==.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*!
Feb 09 2010
Don wrote:Trass3r wrote:Seems arrays inside structs still are not compared with ==.Not in D2, any more. If a member has an opEquals, it's compared using ==.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*!
Feb 09 2010
On Tue, 09 Feb 2010 14:58:13 -0500, grauzone <none example.net> wrote:Don wrote:http://d.puremagic.com/issues/show_bug.cgi?id=3789Trass3r wrote:Seems arrays inside structs still are not compared with ==.Not in D2, any more. If a member has an opEquals, it's compared using ==.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*!
Feb 09 2010
Ok, what's the rationale?Structs are compared *bitwise*!Not in D2, any more. If a member has an opEquals, it's compared using ==.
Feb 09 2010
Trass3r wrote: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.Ok, what's the rationale?Structs are compared *bitwise*!Not in D2, any more. If a member has an opEquals, it's compared using ==.
Feb 09 2010









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