Bug 9078 – non-static opCall is chosen instead of a default constructor

Status
NEW
Severity
normal
Priority
P3
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2012-11-25T10:26:49Z
Last change time
2024-12-13T18:03:04Z
Assigned to
No Owner
Creator
void.unsigned
Moved to GitHub: dmd#18499 →

Comments

Comment #0 by void.unsigned — 2012-11-25T10:26:49Z
[From the thread at: http://forum.dlang.org/thread/[email protected]] The following code: struct A { int i; void opCall(int i) { } } void main() { auto a = A(42); } fails to compile with errors: Error: variable deneme.main.a type void is inferred from initializer opCall(42), and variables cannot be of type void Error: expression opCall(42) is void and has no value A non-static opCall is chosen instead of a default constructor. The non-static opCall() overloads should not interfere with construction.
Comment #1 by maxim — 2012-11-25T11:12:34Z
Probably related: - Issue 6579 Calling static method should *require* using type and not instance, unless specified by author - Issue 6036 Constructor, static opCall and object opCall
Comment #2 by k.hara.pg — 2013-04-03T23:30:10Z
(In reply to comment #0) [snip] > A non-static opCall is chosen instead of a default constructor. > The non-static opCall() overloads should not interfere with construction. Currently function overload resolution does not consider whether the function is static or not (just one exception of the rule is constructor call). For example, function overloading based on static attribute does not work. struct S { void foo() {} static void foo() {} } void main() { S s; s.foo(); // error S.foo(); // error } So, A(42) always invokes opCall, and fails to compile. ------ There is two workarounds. 1. define proper constructor struct A { int i; this(int i) { this.i = i; } void opCall(int i) {} } void main() { auto a = A(42); // OK } 2. Use struct initializer for construction struct A { int i; void opCall(int i) {} } void main() { A a = {42}; // OK a(42); // invoke opCall }
Comment #3 by monarchdodra — 2014-10-03T07:00:55Z
I just hit this again today. It's pretty bad, and it's not very clear either what is going on. Can we do anything to fix this. In particular, I was writting a "functor" style object, but with an opCall that takes no arguments. The reduced example is: struct A { int opCall() { } } void main() { auto a = A(); // !!! } Using "A()" fails to compile. In particular, once you've defined the opCall, it simply becomes *impossible* to declare an instance of your struct, unless you define a constructor, or use A.init. That's all pretty bad.
Comment #4 by robert.schadek — 2024-12-13T18:03:04Z
THIS ISSUE HAS BEEN MOVED TO GITHUB https://github.com/dlang/dmd/issues/18499 DO NOT COMMENT HERE ANYMORE, NOBODY WILL SEE IT, THIS ISSUE HAS BEEN MOVED TO GITHUB