Bug 21107 – Cannot define an r/w property inside a function
Status
RESOLVED
Resolution
DUPLICATE
Severity
normal
Priority
P1
Component
dmd
Product
D
Version
D2
Platform
x86_64
OS
Linux
Creation time
2020-08-04T03:09:29Z
Last change time
2020-08-04T10:25:18Z
Assigned to
No Owner
Creator
Victor Porton
Comments
Comment #0 by porton — 2020-08-04T03:09:29Z
It wonders me that the following program does not compile:
void main() {
@property int x() { return 0; }
@property void x(int) { }
}
x.d(3): Error: declaration x is already defined
Is it a DMD v2.090.1 bug?
If it should not be compilable indeed, it is nevertheless a bug: The error message should be more clear.
Comment #1 by simen.kjaras — 2020-08-04T07:50:48Z
*** This issue has been marked as a duplicate of issue 12578 ***
Comment #2 by porton — 2020-08-04T10:03:51Z
This was marked duplicate as a duplicate of issue 12578. But I suggest to fix this issue (namely with property functions) independently on whether 12578 will be fixed. This issue can be fixed even is 12578 cannot or hard to fix (in the general case), because property functions are not "in fact" overloaded (in the call syntax).
Comment #3 by simen.kjaras — 2020-08-04T10:25:18Z
There is nothing special about @property functions in terms of overload resolution - they use the exact same code as other functions. For instance, this is perfectly valid:
@property
int fun() { return 0; }
@property
int fun(int value) { return 1; }
@property
int fun(string value) { return 2; }
unittest {
auto a = fun;
fun = 13;
fun = "twenty-seven";
}
Fixing this issue for @property functions only will require duplicating code, complicating the compiler. It will also require fixing the exact issue in 12578, and then choosing not to apply the fix to other functions.
An argument could be made that the error message could be better. At least, it could be made to match the error message for same-name globals:
int i;
string i; // variable foo.i conflicts with variable foo.i at foo.d(1)