Comment #0 by bearophile_hugs — 2010-04-02T17:03:42Z
This is a D2 program:
struct Foo {
this(int x=0) {}
static void opCall(int x, int y) {}
}
void main() {
auto f2 = Foo(1); // line 6, OK
auto f3 = Foo(1, 2); // line 7, Err, opCall not found
auto f1 = Foo(); // line 8, Err, conflicts with opCall again
}
dmd 2.042 gives:
test.d(7): Error: constructor test.Foo.this (int x = 0) is not callable using argument types (int,int)
test.d(7): Error: expected 1 arguments, not 2 for non-variadic function type ref Foo(int x = 0)
test.d(8): Error: function test.Foo.opCall (int x, int y) is not callable using argument types ()
test.d(8): Error: expected 2 function arguments, not 0
test.d(8): Error: variable test.main.f1 voids have no value
test.d(8): Error: expression opCall() is void and has no value
My suggestion is in structs to disallow opCall() if a this() is present.
This makes the program more tidy, and avoids that conflict with opCall when Foo() is used with no arguments.
Comment #1 by destructionator — 2011-01-06T05:18:58Z
An important addition to the suggestion: *static* opCall is the real problem here. There should be no problem between an instance opCall and a constructor.
Comment #2 by bearophile_hugs — 2011-03-08T00:32:29Z
This (modified) example from Tom shows problems with not static opCall too:
struct Foo {
int i;
this(int i) { this.i = i; }
void opCall(int x, int y) {}
}
void main() {
Foo foo;
foo(1, 2);
}
Comment #3 by bearophile_hugs — 2011-03-08T09:53:38Z
See also bug 4253
Comment #4 by kennytm — 2011-04-28T11:41:16Z
Probably the same as issue 1840.
Comment #5 by k.hara.pg — 2011-06-21T07:03:40Z
*** This issue has been marked as a duplicate of issue 6036 ***