D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 7075 - overloading opAssign for classes is poorly specified
Summary: overloading opAssign for classes is poorly specified
Status: RESOLVED FIXED
Alias: None
Product: D
Classification: Unclassified
Component: dlang.org (show other issues)
Version: D2
Hardware: All All
: P2 normal
Assignee: Andrej Mitrovic
URL:
Keywords: pull, spec
Depends on:
Blocks:
 
Reported: 2011-12-06 20:32 UTC by Jesse Phillips
Modified: 2014-04-23 12:06 UTC (History)
5 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description Jesse Phillips 2011-12-06 20:32:40 UTC
The table states that classes do not overload the assignment operator. However this code compiles:

class A {
    int a;
    string b;
    float c;
    
    void opAssign(B b) {
       a = b.a;
    }
}

class B {
    int a;
}
void main()
{
    auto a = new A();
    a.a = 5;
    auto b = new B();
    b.a = 10;
    a = b;

    assert(a.a == 10);
}
Comment 1 Jesse Phillips 2011-12-06 20:36:26 UTC
Forgot the pages:

http://www.d-programming-language.org/struct

and this one only mentions struct:

http://www.d-programming-language.org/operatoroverloading.html#Assignment
Comment 2 Steven Schveighoffer 2011-12-07 07:46:59 UTC
No, the code isn't invalid, the documentation is just bad.

identity assignment overloading is not allowed for classes, that is what the table is alluding to:

class A
{
   void opAssign(A a) {}
}

Error: function testassign.A.opAssign identity assignment operator overload is illegal

There are several other places in the spec that could be better written.
Comment 3 Walter Bright 2012-04-28 02:13:22 UTC
Doc bugs aren't critical.
Comment 4 Andrej Mitrovic 2014-04-23 11:52:57 UTC
The class overload page seems to be improved now, but the table on the structs pages lists "assign overload" rather than the better-describing "identity assign overload".

https://github.com/D-Programming-Language/dlang.org/pull/562
Comment 5 github-bugzilla 2014-04-23 12:06:06 UTC
Commits pushed to master at https://github.com/D-Programming-Language/dlang.org

https://github.com/D-Programming-Language/dlang.org/commit/fee0b277069a2c0bcf5fc0b3f3370197e13a6163
Fix Issue 7075 - Use better wording for identity assignment overload.

https://github.com/D-Programming-Language/dlang.org/commit/beece10c7eedd0e0f7416b104cc6ab8889e60792
Merge pull request #562 from AndrejMitrovic/Fix7075

[Trivial] Issue 7075 - Use better wording for identity assignment overload.