default '==' on structs

spir denis.spir at gmail.com
Wed Feb 2 11:10:59 PST 2011


On 02/02/2011 07:09 PM, bearophile wrote:
>> Lars is right, the == among structs is broken still:
>
> If necessary please open a new bug report, this is an important bug.
>
> Bye,
> bearophile

Right, reduced the bug cases I found to:

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") );
}

Same for array members (indeed):

struct A {int[] a;}
unittest {
     // concat
     int[] a1 = [1,2]; int[] a2 = [3];
     int[] a3 = [1]; int[] a4 = [2,3];
     assert ( A(a1 ~ a2) != A(a3 ~ a4) );
     // slice
     int[] a = [1,2,3];
     assert ( A(a[1..$-1]) != A([2]) );
}

But this is not very relevant, because plain arrays /members/ (unlike strings) 
seem to be compared by ref (exactly by "array struct"):

unittest {
     // string
     string s1 = "hello"; string s2 = "hello";
     assert ( S(s1) == S(s2) );
     // array (note '!=' assert)
     int[] a1 = [1,2,3]; int[] a2 = [1,2,3];
     assert ( A(a1) != A(a2) );
}

I think at opening a new bug report in a short while, with reference to issue 
#3433 (http://d.puremagic.com/issues/show_bug.cgi?id=3433) which was (unduly?) 
marked as fixed for dmd 2.037. In the meanwhile, if anyone knows about related 
cases of bug, or has more info, please tell.

On the other hand, the example of arrays let me doubt about correct / desirable 
semantics.
1. Indeed, I think string members should be compared by value.
2. But arrays are not, so should strings be compared by ref as well, if only to 
avoid inconsistency?
3. But then, why the already existing difference between strings & arrays?
4. Or should arrays be compared by value like string?
5. But strings are not /really/ compared by value as of now...
The current behaviour is weird. I don't how it can only happen.

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



More information about the Digitalmars-d-learn mailing list