Bug 7206 – Constructor from mixin does not conflict with other constructors
Status
RESOLVED
Resolution
DUPLICATE
Severity
normal
Priority
P2
Component
dmd
Product
D
Version
D2
Platform
Other
OS
All
Creation time
2012-01-02T14:10:55Z
Last change time
2020-09-17T21:29:49Z
Keywords
accepts-invalid, diagnostic
Assigned to
No Owner
Creator
Robert Clipsham
Comments
Comment #0 by robert — 2012-01-02T14:10:55Z
----
import std.stdio;
mixin template foo() {
this() {
writefln("a");
}
}
class A {
mixin foo!();
this() {
writefln("b");
}
version(ErrorsAsExpected) {
this() {
writefln("c");
}
}
}
void main() {
A a = new A;
}
----
When the above is compiled, no error is given, however when -version=ErrorsAsExpected is given an error occurs. Tested with dmd 2.057.
Comment #1 by robert — 2012-01-02T14:13:34Z
I forgot to mention, when ErrorsAsExpected is not defined, this prints b, the constructor from the mixin is disregarded.
Comment #2 by timon.gehr — 2012-01-02T14:24:56Z
This is by design:
Spec: Mixin Scope
The declarations in a mixin are 'imported' into the surrounding scope. If the
name of a declaration in a mixin is the same as a declaration in the
surrounding scope, the surrounding declaration overrides the mixin one.
Comment #3 by hoganmeier — 2012-01-02T14:42:28Z
Imho it isn't right though to silently accept this.
There should at least be a warning that the code doesn't work as the general programmer would expect and maybe give a hint that you can achieve overload resolution with an additional alias.
Otherwise it heavily depends on your code if you are lucky and get an error message like in http://d.puremagic.com/issues/show_bug.cgi?id=3332 or unexpected behavior.
This is not limited to constructors. Overload resolution in general isn't easily possible.
I have to resort to ugly string mixins just because of this.
Comment #4 by lovelydear — 2012-04-19T12:38:06Z
PS E:\DigitalMars\dmd2\samples> rdmd bug.d
b
PS E:\DigitalMars\dmd2\samples> rdmd -version=ErrorsAsExpected bug.d
bug.d(22): Error: constructor bug.A.this called with argument types:
(())
matches both:
bug.A.this()
and:
bug.A.this()
PS E:\DigitalMars\dmd2\samples>
Comment #5 by simen.kjaras — 2020-09-17T21:29:49Z
*** This issue has been marked as a duplicate of issue 3332 ***