www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 6036] New: Constructor, static opCall and object opCall

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

           Summary: Constructor, static opCall and object opCall
           Product: D
           Version: D2
          Platform: All
        OS/Version: All
            Status: NEW
          Keywords: accepts-invalid, rejects-valid
          Severity: normal
          Priority: P2
         Component: DMD
        AssignedTo: nobody puremagic.com
        ReportedBy: k.hara.pg gmail.com



Created an attachment (id=980)
Test cases for this issue.

----
struct S{ ... }
S(...)  // (a)
S s;
s(...)  // (b)
----
We should lookup them following order:

(a) From type
1. Constructor call
   Constructors are defined with arguments (>= 1), and hide any signature of
static opCalls.
2. Static opCall
   If no constructors exists, static opCalls are checked.
3. When matching with 1 and 2 failed, struct literal syntax is checked.
4. Otherwise makes error.

(b) From object
1. Matchings are tested against static opCalls and object (=non-static)
opCalls.
2. No opCalls makes error.

These rules reject following current dmd bugs:
- Constructors are visible from object.
  This problem pointed out by issue 1840, issue 4053 and issue 4253.
- Object opCalls are visible from type.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
May 19 2011
next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=6036


bearophile_hugs eml.cc changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |bearophile_hugs eml.cc



This is an important issue.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
May 19 2011
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=6036


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

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Keywords|                            |patch
           See Also|                            |http://d.puremagic.com/issu
                   |                            |es/show_bug.cgi?id=4053



Posted patch as pull request:
https://github.com/D-Programming-Language/dmd/pull/72

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
May 19 2011
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=6036




*** Issue 4253 has been marked as a duplicate of this issue. ***


*** Issue 4053 has been marked as a duplicate of this issue. ***

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Jun 21 2011
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=6036


wfunction hotmail.com changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |wfunction hotmail.com



An instance-level opCall should not disable the regular constructor.

Test case:

struct Adder {
    int v;
    auto opCall(int x) { return x + v; }
}

auto adder(int v) {
    return Adder(v);  // How do I call the constructor??
}

int main() {
    auto a = adder(5);
}

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Sep 26 2011
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=6036


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

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |steve.teale britseyeview.co
                   |                            |m



*** Issue 2916 has been marked as a duplicate of this issue. ***

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Jan 23 2012
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=6036




See also Issue 4678  and Issue 7210 and Issue 1840

This is a problem I hit often.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
May 27 2012
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=6036




*** Issue 4678 has been marked as a duplicate of this issue. ***

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
May 30 2012
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=6036


jens.k.mueller gmx.de changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |jens.k.mueller gmx.de



This bug makes using opCall practically useless.
As soon as you have a constructor you get a compile error when you want to call
opCall using the bracket syntax.
E.g. the following code does not compile.

struct F {
  this(int a) { }
  int opCall(int x, int y)
  {
      return 1;
  }
}

unittest
{
  F f;
  int i;

  i = f(3,4,5);
  // Error: constructor F.this (int a) is not callable using argument
  // types (int,int,int)
  // workaround: i = f.opCall(3,4,5);
}

This is v2.060 on Linux.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Aug 14 2012
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=6036




Commits pushed to master at https://github.com/D-Programming-Language/dmd

https://github.com/D-Programming-Language/dmd/commit/c8aba4a80895a7fcab9b1e023fa336d4012b6c05
fix Issue 6036 - Constructor, static opCall and object opCall

https://github.com/D-Programming-Language/dmd/commit/098096c3b38f1cd0e8feb41caebbbb2f0f59b8fe


Issue 6036 - Constructor, static opCall and object opCall

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Oct 06 2012
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=6036






 struct Adder {
     int v;
     auto opCall(int x) { return x + v; }
 }
 
 auto adder(int v) {
     return Adder(v);  // How do I call the constructor??
 }
 
 int main() {
     auto a = adder(5);
 }
A little reduced: struct Adder { int v; int opCall(int x) { return x + v; } } void main() { auto a = Adder(5); } It gives: temp.d(6): Error: need 'this' for opCall type int(int x) Is it expected that the instance opCall disables the implicitly created struct ctor? -- Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email ------- You are receiving this mail because: -------
Oct 06 2012
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=6036




---


 
 A little reduced:
 
 struct Adder {
     int v;
     int opCall(int x) { return x + v; }
 }
 void main() {
     auto a = Adder(5);
 }
 
 
 It gives:
 
 temp.d(6): Error: need 'this' for opCall type int(int x)
 
 Is it expected that the instance opCall disables the implicitly created struct
 ctor?
At least it is same as before merging my pull, except small error message difference. It is still a corner case of language spec, but I think it is an expected behavior. Because, the operator overloading mechanism just consider whether the function named opXXXX exists or not, and doesn't consider whether the opXXXX is really static or not. -- Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email ------- You are receiving this mail because: -------
Oct 06 2012
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=6036


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

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



---
*** Issue 7210 has been marked as a duplicate of this issue. ***

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Oct 06 2012
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=6036






 It is still a corner case of language spec, but I think it is an expected
 behavior.
 Because, the operator overloading mechanism just consider whether the function
 named opXXXX exists or not, and doesn't consider whether the opXXXX is really
 static or not.
See a discussion thread: http://forum.dlang.org/thread/rwypxtvosdhjiwrtzwfi forum.dlang.org -- Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email ------- You are receiving this mail because: -------
Oct 07 2012
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=6036


yebblies <yebblies gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |RESOLVED
                 CC|                            |yebblies gmail.com
         Resolution|                            |FIXED



https://github.com/D-Programming-Language/dmd/commit/098096c3b38f1cd0e8feb41caebbbb2f0f59b8fe

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Oct 07 2012
prev sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=6036


Vladimir Panteleev <thecybershadow gmail.com> changed:

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



08:12:53 EEST ---
There is still a problem with the default constructor (a non-static opCall is
chosen instead of it), see Issue 9078.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Apr 02 2013