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



---
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




---
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




 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




---
 
 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



04:12:04 EET ---

 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.
 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




---
 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





 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.
 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