Comment #0 by bearophile_hugs — 2010-11-01T13:24:03Z
This is wrong D2 code, it contains a common mistake (f is a Foo instead of the correct Foo*):
struct Foo {
int x;
this(int x_) {
this.x = x_;
}
}
void main() {
Foo f = new Foo(0);
}
DMD 2.050 gives the error messages that don't look correct:
test.d(8): Error: constructor test3.Foo.this (int x_) is not callable using argument types (Foo*)
test.d(8): Error: cannot implicitly convert expression (new Foo(0)) of type Foo* to int
But I was expecting a single error message similar to:
test.d(8): Error: cannot implicitly convert expression (new Foo(0)) of type Foo* to Foo
Comment #1 by andrej.mitrovich — 2014-04-28T12:01:08Z
It's not a wrong diagnostic, but maybe it can be improved somehow. The issue is that after construction the compiler will attempt another construction. E.g.:
-----
struct Foo
{
this(Foo* x_) { }
this(int x_) { }
}
void main()
{
// calls two ctors
Foo f = new Foo(0);
}
-----
Maybe this can be considered dangerous behavior. I'll CC Kenji for thoughts.
Comment #2 by razvan.nitu1305 — 2018-04-23T12:20:26Z