default '==' on structs

spir denis.spir at gmail.com
Wed Feb 2 11:29:35 PST 2011


On 02/02/2011 07:41 PM, spir wrote:
> On 02/02/2011 07:05 PM, bearophile wrote:
>> spir:
>>
>>> * The issue reported is about '==' on structs not using member opEquals when
>>> defined, instead performing bitwise comparison. This is not my case: Lexeme
>>> members are plain strings and an uint. They should just be compared as is.
>>> Bitwise comparison should just work fine.
>>> Also, this issue is marked solved for dmd 2.037 (I use 2.051).
>>
>> Lars is right, the == among structs is broken still:
>>
>> struct Foo { string s; }
>> void main() {
>> string s1 = "he";
>> string s2 = "llo";
>> string s3 = "hel";
>> string s4 = "lo";
>> auto f1 = Foo(s1 ~ s2);
>> auto f2 = Foo(s3 ~ s4);
>> assert((s1 ~ s2) == (s3 ~ s4));
>> assert(f1 == f2);
>> }
>
> Thank you, this helps much. I don't get the details yet, but think some similar
> issue is playing a role in my case. String members of the compared Lexeme
> structs are not concatenated, but one of them is sliced from the scanned source.
> If I dup'ed instead of slicing, this would create brand new strings; thus '=='
> performing bitwise comp should run fine, don't you think? I'll try in a short
> while.

No! idup does not help, still need opEquals. See also this example case:

struct S {string s;}
unittest {
     // concat
     string s1 = "he"; string s2 = "llo";
     string s3 = "hel"; string s4 = "lo";
     assert ( S(s1 ~ s2) != S(s3 ~ s4) );
     // slice
     string s = "hello";
     assert ( S(s[1..$-1]) != S("ell") );
     // idup'ed
     assert ( S(s[1..$-1].idup) != S("ell") );
     s2 = s[1..$-1].idup;
     assert ( S(s2) != S("ell") );
}

Denis
-- 
_________________
vita es estrany
spir.wikidot.com



More information about the Digitalmars-d-learn mailing list