[Issue 24279] New: Conflicting constructors/functions due to default arguments should not compile

d-bugmail at puremagic.com d-bugmail at puremagic.com
Tue Dec 12 17:06:49 UTC 2023


https://issues.dlang.org/show_bug.cgi?id=24279

          Issue ID: 24279
           Summary: Conflicting constructors/functions due to default
                    arguments should not compile
           Product: D
           Version: D2
          Hardware: x86
                OS: Windows
            Status: NEW
          Severity: normal
          Priority: P1
         Component: dmd
          Assignee: nobody at puremagic.com
          Reporter: HuskyNator at protonmail.ch

When constructors or functions conflict due to having the same calling
signature, they should not be allowed to compile.

This is already the case in most scenarios, but when default arguments cause
conflict, the compiler does not catch this issue. Even more so, in the case of
constructors, visibility rules are ignored.

Example:
```d
module text;
import std.stdio;
class Text {
        string text;
        private this(string text) {
                writeln("private");
        }
        this(string filename, string arg2 = ".txt") {
                writeln("public");
        }
}
```
```d
module app;
void main(){
    Text t = new Text("file"); // writes "private"
}
```

Calling `new Text("test")` from anywhere will call the first constructor,
ignoring its `private` attribute.

If this is done with functions instead of constructors, the same thing will
happen with one exception: the compiler will state the function is not
accessible. (It will however try to resolve to the private function, ignoring
the accessible alternative)

1. This hints at issues with constructors ignoring visibility attributes!
2. The code should not compile when conflicts occur (even when this is due to
default values).

--


More information about the Digitalmars-d-bugs mailing list