In DMD 2.032, when a struct was assigned to at it's declaration point, opAssign overloads were called if other alternative were not available. In DMD 2.033, neither opAssign or ctor overloads are called, only static opCall works
struct Foo {
int x;
// static opCall(int v) { // Un-comment this and the below works
// Foo f;
// f.x = v;
// return f;
// }
this(int v){ x = v; }
void opAssign(int v){
x = v;
return this;
}
}
void main(char[][] args) {
int y = 5;
Foo f = y; // fails
f = y; // okay
f = Foo(y); // okay
}
Comment #1 by clugdbug — 2009-10-06T01:14:23Z
My fault. This is a consquence of the fix to bug 2702. Previously it used to accept any old garbage inside a struct initializer; now it only accepts static opCall. It should definitely allow constructors as well.
Based on my reading of the spec, the fact that opAssign used to work seems to have been a bug:
"assignment is defined as copying the contents of one object over another, already initialized, type" (struct.html)
Comment #2 by clugdbug — 2009-10-29T12:58:44Z
PATCH: declaration.c, line 1094.
{
/* Look for opCall
* See bugzilla 2702 for more discussion
*/
Type *ti = ei->exp->type->toBasetype();
------- ADD THIS CODE:
// Look for ctor
if (sd->ctor &&
/* Initializing with the same type is done differently
*/
!(ti->ty == Tstruct && t->toDsymbol(sc) == ti->toDsymbol(sc))) {
// Rewrite as e1.call(arguments)
Expression * eCall = new DotIdExp(loc, e1, Id::ctor);
ei->exp = new CallExp(loc, eCall, ei->exp);
}
else
-----------
// Don't cast away invariant or mutability in initializer
if (search_function(sd, Id::call) &&
Comment #3 by leandro.lucarella — 2009-11-04T06:39:53Z