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

d-bugmail at puremagic.com d-bugmail at puremagic.com
Sun Oct 6 01:18:50 PDT 2013


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



--- Comment #5 from monarchdodra at gmail.com 2013-10-06 01:18:48 PDT ---
(In reply to comment #2)
> 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.

I think this is completely bogus. Doing this means an alias this takes
precedence over the type that holds it, and it should *never* happen. An alias
this should never ever EVER take precedence over the base type.

Here are some rebuttals to the above mentioned behavior.

#1 alias this is designed to emulate "inheritance" for structs. EG: *extending*
a struct. Today, it was mostly hijacked to instead do implicit conversion,
where a struct can pass for some other type.

The very "standard" scenario of:

//----
struct Base
{
    int i;
}
struct Derived
{
    Base b;
    alias b this;
    int j;
}

void main()
{
    Base b = {0};
    Derived d1 = {b, 1};
    Derived d2 = {b, 2};
    assert(d1 != d2); //This fails
}
//----

The above assert is going to fail. This is completely unacceptable. Derived is
a "subkind" of Base: It's Base plus some more. With the current behavior
though, Derived is straight up treated as a Base, and everything that makes it
a Derived is forgotten.

#2. opAssign doesn't follow this rule. I'm not going to go into more details on
this - I think it's obvious that opAssign and opEqual should follow the same
set of rules.

#3. If a struct has multiple "alias this", then comparison will probably fail
spectacualrly.

#4. Having a template called "hasElaborateEqual" would be virtually impossible
to implement. Indeed, only the type that is resolved by alias this should be
taken into account.

Because of the above reason, I think that "alias this" most certainly should
*not* be considered before checking if "T == T" is legal.

Heck, I don't think it should be considered at *all* for "T == T": opAssign
doesn't check.

-- 
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