[Issue 12194] Constructor prevents static opCall regression?
d-bugmail at puremagic.com
d-bugmail at puremagic.com
Tue Feb 18 18:38:58 PST 2014
https://d.puremagic.com/issues/show_bug.cgi?id=12194
--- Comment #7 from Kenji Hara <k.hara.pg at gmail.com> 2014-02-18 18:38:54 PST ---
This change has been introduced to fix the language design issue discovered by
bug 12070 (the diagnostic message is added by issue 12124).
In D1 age, historically static opCall was a hack to use C++ like syntax for
object construction.
In D2, It has not been recommended because constructor is properly supported
for structs.
Until 2.064, constructor takes precedence over static opCall _in most cases_.
For example, if you mix constructors and static opCall that take arguments,
static opCall is always hidden.
struct S {
this(int n) {}
static void opCall(int n, int m) {}
}
void main() {
auto s = S(1); // call this(int)
auto s = S(1, 2); // error, this(int) is not callable using two int
arguments
}
But if static opCall has no arguments, it had been allowed for S(), like as
OP-code.
----
However, the language behavior will cause a problem if opCall method is not
static. Following is the sample code that summarize bug 12070.
struct S {
this(int n) {}
void opCall() {}
}
void main() {
auto s = S(); // need 'this' for S.opCall()
}
Note the behavior that S.opCall() will match to the instance opCall.
D does not have the feature to resolve function overloading based on the
'static' attribute, as same as C++.
Although it is definitely legitimate to define both regular constructors and
non-static opCall, the code had not work contrary to the expectation until
2.064.
>From 2.065, in order to fix the design issue, the syntax `Type()` is always
reserved for default construction if one or more user defined constructors
exist.
And it will conform to the D2 intention that using static opCall for
construction has not been recommended.
--
Configure issuemail: https://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
More information about the Digitalmars-d-bugs
mailing list