[Issue 11161] Document the default struct equality comparison and operator overloading

d-bugmail at puremagic.com d-bugmail at puremagic.com
Thu Oct 3 02:45:43 PDT 2013


http://d.puremagic.com/issues/show_bug.cgi?id=11161


Kenji Hara <k.hara.pg at gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Keywords|wrong-code                  |spec
          Component|DMD                         |websites
            Summary|If `opEquals` is not        |Document the default struct
                   |defined, compiler should    |equality comparison and
                   |translate struct objects    |operator overloading
                   |equality to member-wise     |
                   |equality even if there is   |
                   |`alias this`                |


--- Comment #2 from Kenji Hara <k.hara.pg at gmail.com> 2013-10-03 02:45:37 PDT ---
(In reply to comment #0)
> ---
> struct S {
>     bool opEquals(ref const S) { return false; } 
> }
> struct T {
>     S s;
>     int value;
>     alias value this;
> }
> 
> void main() {
>     T t1, t2;
>     assert(t1.tupleof != t2.tupleof);
>     assert(t1 != t2); // fails
> }
> ---
> 
> If this behavior is expected, please write the reasons shortly and change it to
> documentation issue unless this feature is documented.

For the expression `t1 != t2`, the semantic analysis is done in the following
order:

1. Find opEquals method from the type of operands
2. If 'alias this' declaration exists, compiler tries to resolve equality
operation via alias this. -> `t1.value != t2.value` is tested, and succeeds to
compile.
3. If any opEquals and comparable 'alias this' is not found, the struct
equality comparison is rewritten to their each field comparison.

The point is, #1 and #2 are the part of "operator overloading" feature, and it
is precedence than the default struct equality operation (== field-wise
comparison). Historically

Historically:
- By bug 3789, the default comparison behavior was changed from bitwise to
  member-wise.
- By bug 10037, the operator overload resolution precedence order was fixed
  (default member-wise comparison does not hide explicitly defined alias this).

Therefore, current behavior is expected.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------


More information about the Digitalmars-d-bugs mailing list