Bug 12277 – static opCall is hidden by @disabled constructors and can never be called
Status
RESOLVED
Resolution
WONTFIX
Severity
enhancement
Priority
P2
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2014-02-28T04:59:00Z
Last change time
2017-07-21T08:41:47Z
Assigned to
nobody
Creator
remotion4d
Comments
Comment #0 by remotion4d — 2014-02-28T04:59:12Z
struct S
{
@disable this();
@disable this(this);
this(int i){ }
static S opCall(){
S s = S(123);
return s;
}
}
This fails to compile with "static opCall is hidden by constructors and can never be called".
But the default ctor is disabled.
Comment #1 by nick — 2014-07-24T16:09:59Z
Note that the error on your code is correct as you define this(int) - see bug 12194.
This issue is not the same as that one, as @disable this alone triggers the error:
struct S
{
@disable this();
static S opCall(){
return S.init;
}
}
Comment #2 by dlang-bugzilla — 2017-07-21T08:41:47Z
(In reply to Remo from comment #0)
> But the default ctor is disabled.
Fairly sure that's not relevant.
As I understand, @disable does not make a symbol invisible to the compiler, it makes it so that invoking it (one way or another) becomes forbidden. So, annotating the constructor with @disable will not make it prefer the static opCall.
I suppose that, as an enhancement, the compiler could be made to prefer the static opCall when the constructor is present but disabled, but this is likely to complicate the language/implementation for very little gain, especially considering that the simple workaround of writing a factory function (as the error message suggests) is available.
Note that if you give the opCall method arguments (e.g. `static S opCall(int)`), the code is accepted, so the problem occurs only with the questionable ambiguous S() syntax.
As this was filed and has been inactive for over 3 years ago, I'll close this, but feel free to reopen if you disagree.