D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 7206 - Constructor from mixin does not conflict with other constructors
Summary: Constructor from mixin does not conflict with other constructors
Status: RESOLVED DUPLICATE of issue 3332
Alias: None
Product: D
Classification: Unclassified
Component: dmd (show other issues)
Version: D2
Hardware: Other All
: P2 normal
Assignee: No Owner
URL:
Keywords: accepts-invalid, diagnostic
Depends on:
Blocks:
 
Reported: 2012-01-02 14:10 UTC by Robert Clipsham
Modified: 2020-09-17 21:29 UTC (History)
4 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description Robert Clipsham 2012-01-02 14:10:55 UTC
----
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 Robert Clipsham 2012-01-02 14:13:34 UTC
I forgot to mention, when ErrorsAsExpected is not defined, this prints b, the constructor from the mixin is disregarded.
Comment 2 timon.gehr 2012-01-02 14:24:56 UTC
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 Trass3r 2012-01-02 14:42:28 UTC
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 SomeDude 2012-04-19 12:38:06 UTC
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 Simen Kjaeraas 2020-09-17 21:29:49 UTC

*** This issue has been marked as a duplicate of issue 3332 ***