digitalmars.D.learn - compare struct with string member is wield;
- Cheng Wei (11/11) Oct 11 2011 struct S {
- Jonathan M Davis (4/18) Oct 11 2011 It succeeds on my box (Linux 64) with the latest from git. I assume that...
- Cheng Wei (16/16) Oct 11 2011 Sorry. The previous is not the one causes the problem.
- Jonathan M Davis (7/27) Oct 11 2011 Yeah. It's failing for me too. It's obviously a bug of some kind. I'd ha...
- Cheng Wei (2/2) Oct 11 2011 Thanks. Removing the ';' after struct and class is really helpful. The
- Dmitry Olshansky (16/43) Oct 12 2011 It is strange. The reason could be: by default for structs it does
struct S { string str; }; S g_s; unittest { S s1; S s2; assert(s1 == s2); // Success assert(g_s == s1); // Failed } Is this expected? If so, may I know the reason? Thanks.
Oct 11 2011
On Wednesday, October 12, 2011 05:16:40 Cheng Wei wrote:struct S { string str; }; S g_s; unittest { S s1; S s2; assert(s1 == s2); // Success assert(g_s == s1); // Failed } Is this expected? If so, may I know the reason? Thanks.It succeeds on my box (Linux 64) with the latest from git. I assume that you're using 2.055? Maybe it's a bug that was fixed since the release. - Jonathan M Davis
Oct 11 2011
Sorry. The previous is not the one causes the problem. Try this: struct S { string str = "Hello"; // Adding an initial value here. }; S g_s; unittest { S s1; S s2; assert(s1 == s2); // Success assert(g_s == s1); // Fail auto s3 = g_s; assert(s3 == g_s);; // Even this will fail. } It seems if the string is initialized with a default value, then this does not work.
Oct 11 2011
On Wednesday, October 12, 2011 06:07:38 Cheng Wei wrote:Sorry. The previous is not the one causes the problem. Try this: struct S { string str = "Hello"; // Adding an initial value here. }; S g_s; unittest { S s1; S s2; assert(s1 == s2); // Success assert(g_s == s1); // Fail auto s3 = g_s; assert(s3 == g_s);; // Even this will fail. } It seems if the string is initialized with a default value, then this does not work.Yeah. It's failing for me too. It's obviously a bug of some kind. I'd have to going digging through d.puremagic.com/issues to see whether anything like it has been reported though. By the way, the semicolon at the end of the definition of S is unnecessary in D. - Jonathan M Davis
Oct 11 2011
Thanks. Removing the ';' after struct and class is really helpful. The ";" keeps trapping me in C++ coding. :)
Oct 11 2011
On 12.10.2011 10:14, Jonathan M Davis wrote:On Wednesday, October 12, 2011 06:07:38 Cheng Wei wrote:It is strange. The reason could be: by default for structs it does bitlevel memcmp style comparison. Now in this case it will compare pointer-length pairs directly, if somehow global var got different string (address-wise) then it won't compare equal to S.init no matter what. So two things: - print out .str.ptr for all of you vars to see if that's the case - define sane opEquals*: bool opEquals(in S s)const{ return str == t.str; } * I haven't followed the timeline of the changes to opEquals that ultimately made things much simpler so you may have to rewrite it as bool opEquals(const ref S s)const in dmd 2.055. -- Dmitry OlshanskySorry. The previous is not the one causes the problem. Try this: struct S { string str = "Hello"; // Adding an initial value here. }; S g_s; unittest { S s1; S s2; assert(s1 == s2); // Success assert(g_s == s1); // Fail auto s3 = g_s; assert(s3 == g_s);; // Even this will fail. } It seems if the string is initialized with a default value, then this does not work.Yeah. It's failing for me too. It's obviously a bug of some kind. I'd have to going digging through d.puremagic.com/issues to see whether anything like it has been reported though. By the way, the semicolon at the end of the definition of S is unnecessary in D. - Jonathan M Davis
Oct 12 2011