www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 12120] New: Static opCall for structures skipped (Github HEAD)

reply d-bugmail puremagic.com writes:
https://d.puremagic.com/issues/show_bug.cgi?id=12120

           Summary: Static opCall for structures skipped (Github HEAD)
           Product: D
           Version: D2
          Platform: All
        OS/Version: All
            Status: NEW
          Severity: regression
          Priority: P2
         Component: DMD
        AssignedTo: nobody puremagic.com
        ReportedBy: puneet coverify.org


--- Comment #0 from Puneet Goel <puneet coverify.org> 2014-02-09 10:32:02 PST
---
Here is the reduced code.

struct Foo {
  this(int) {}
  static Foo opCall() {
    import std.stdio;
    writeln("opCall");
    Foo foo = Foo(0);
    return foo;
  }
}

void main() {
  Foo foo = Foo();
}

-- 
Configure issuemail: https://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Feb 09 2014
next sibling parent d-bugmail puremagic.com writes:
https://d.puremagic.com/issues/show_bug.cgi?id=12120



--- Comment #1 from Puneet Goel <puneet coverify.org> 2014-02-09 10:34:16 PST
---
Seems to be due to fix12070.

-- 
Configure issuemail: https://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Feb 09 2014
prev sibling next sibling parent d-bugmail puremagic.com writes:
https://d.puremagic.com/issues/show_bug.cgi?id=12120


Kenji Hara <k.hara.pg gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |RESOLVED
         Resolution|                            |INVALID


--- Comment #2 from Kenji Hara <k.hara.pg gmail.com> 2014-02-09 17:30:31 PST ---
(In reply to comment #0)
 Here is the reduced code.
 
 struct Foo {
   this(int) {}
   static Foo opCall() {
     import std.stdio;
     writeln("opCall");
     Foo foo = Foo(0);
     return foo;
   }
 }
 
 void main() {
   Foo foo = Foo();
 }

If you define constructors in a struct, the syntax `Type()` should be reserved for default construction. If you want to support both Foo() and Foo(1), you should change the constructor to static Foo opCall(int). -- Configure issuemail: https://d.puremagic.com/issues/userprefs.cgi?tab=email ------- You are receiving this mail because: -------
Feb 09 2014
prev sibling next sibling parent d-bugmail puremagic.com writes:
https://d.puremagic.com/issues/show_bug.cgi?id=12120



--- Comment #3 from Puneet Goel <puneet coverify.org> 2014-02-09 18:04:52 PST
---
 
 If you define constructors in a struct, the syntax `Type()` should be reserved
 for default construction.
 
 If you want to support both Foo() and Foo(1), you should change the constructor
 to static Foo opCall(int).

I am sorry if I missed something, but how do I initialize a struct object element from a call to Foo()? I believe default constructors are not allowed for structs. Say: class Bar {} struct Foo { Bar bar; // bar is not static static Foo opCall() { // I want to return a Foo object with bar initialized // How can I achieve that?? } this() { // Does not compile bar = new Bar(); } } -- Configure issuemail: https://d.puremagic.com/issues/userprefs.cgi?tab=email ------- You are receiving this mail because: -------
Feb 09 2014
prev sibling next sibling parent d-bugmail puremagic.com writes:
https://d.puremagic.com/issues/show_bug.cgi?id=12120


Vladimir Panteleev <thecybershadow gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |thecybershadow gmail.com


--- Comment #4 from Vladimir Panteleev <thecybershadow gmail.com> 2014-02-10
04:12:04 EET ---
(In reply to comment #2)
 If you define constructors in a struct, the syntax `Type()` should be reserved
 for default construction.
 
 If you want to support both Foo() and Foo(1), you should change the constructor
 to static Foo opCall(int).

If I understood this change correctly, I don't think it is a good change, as I mentioned in the pull request: https://github.com/D-Programming-Language/dmd/pull/3221 This issue only serves as further evidence towards that. (In reply to comment #3)
 I am sorry if I missed something, but how do I initialize a struct object
 element from a call to Foo()? I believe default constructors are not allowed
 for structs.

static Foo opCall() { Foo foo; foo.bar = ...; return foo; } -- Configure issuemail: https://d.puremagic.com/issues/userprefs.cgi?tab=email ------- You are receiving this mail because: -------
Feb 09 2014
prev sibling next sibling parent d-bugmail puremagic.com writes:
https://d.puremagic.com/issues/show_bug.cgi?id=12120



--- Comment #5 from Puneet Goel <puneet coverify.org> 2014-02-09 18:25:16 PST
---
 static Foo opCall()
 {
     Foo foo;
     foo.bar = ...;
     return foo;
 }

Thanks Vladimir. Kenji the idea that this syntax "Foo foo = Foo();" would not be supported if another constructor is available is a bit too constraining. -- Configure issuemail: https://d.puremagic.com/issues/userprefs.cgi?tab=email ------- You are receiving this mail because: -------
Feb 09 2014
prev sibling parent d-bugmail puremagic.com writes:
https://d.puremagic.com/issues/show_bug.cgi?id=12120



--- Comment #6 from Kenji Hara <k.hara.pg gmail.com> 2014-02-09 19:35:13 PST ---
(In reply to comment #4)
 If I understood this change correctly, I don't think it is a good change, as I
 mentioned in the pull request:
 https://github.com/D-Programming-Language/dmd/pull/3221
 
 This issue only serves as further evidence towards that.

I opened an enhancement issue 12124. (In reply to comment #5)
 Kenji the idea that this syntax "Foo foo = Foo();" would not be supported if
 another constructor is available is a bit too constraining.

Do complex work with the syntax Foo() is not recommended in D language design. In D, Foo foo = Foo(); is normally equivalent with Foo foo;. It is important in generic code. -- Configure issuemail: https://d.puremagic.com/issues/userprefs.cgi?tab=email ------- You are receiving this mail because: -------
Feb 09 2014