Issue 3789, stucts equality

Alvaro alvaroDotSegura at gmail.com
Mon Mar 26 14:25:07 PDT 2012


El 26/03/2012 13:58, bearophile escribió:
> Issue 3789 is an enhancement request, I think it fixes one small but quite important problem in D design. The situation is shown by this simple code:
>
>
> struct String {
>      char[] data;
> }
> void main () {
>      auto foo = String("foo".dup);
>      auto bar = String("foo".dup);
>      assert(bar !is foo, "structs aren't the same bit-wise");
>      assert(bar == foo, "oops structs aren't equal");
> }
>
>
>
> The D Zen says D is designed to be safe on default and to perform unsafe (and faster) things on request. Not comparing the strings as strings in the following code breaks the Principle of least astonishment, so breaks that rule.
>


Maybe it makes more sense that struct==struct applies == to each of its 
fields. It would be the same as bitwise comparison for simple primitive 
types, but would be more useful with other types such as strings.



More information about the Digitalmars-d mailing list