www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Need 'this' to access 'opCall'? Was I trying to?

reply Mehrdad <wfunction hotmail.com> writes:
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);
}
Sep 26 2011
parent reply Trass3r <un known.com> writes:
Your opCall isn't static.
Sep 26 2011
next sibling parent reply Mehrdad <wfunction hotmail.com> writes:
On 9/26/2011 8:13 AM, Trass3r wrote:
 Your opCall isn't static.

the type.
Sep 26 2011
parent reply Mehrdad <wfunction hotmail.com> writes:
On 9/26/2011 9:40 AM, Trass3r wrote:
 Well, it looks like having declared a non-static opCall makes the
 automatically generated constructor for the struct unusable - which 
 may or may not be a bug

I do think it's a bug. Only a static opCall should interfere with the constructor.

Sep 26 2011
parent Mehrdad <wfunction hotmail.com> writes:
On 9/26/2011 10:34 AM, Steven Schveighoffer wrote:
 On Mon, 26 Sep 2011 13:13:52 -0400, Mehrdad <wfunction hotmail.com> 
 wrote:

 On 9/26/2011 9:40 AM, Trass3r wrote:
 Well, it looks like having declared a non-static opCall makes the
 automatically generated constructor for the struct unusable - which 
 may or may not be a bug

I do think it's a bug. Only a static opCall should interfere with the constructor.


Might want to add this case (as I think it is important, and doesn't yet seem to have an exact duplicate report) to bug 6036. Looks like Kenji is on the case, and he usually comes up with a pull request :) -Steve

http://d.puremagic.com/issues/show_bug.cgi?id=6036
Sep 26 2011
prev sibling next sibling parent Jonathan M Davis <jmdavisProg gmx.com> writes:
On Monday, September 26, 2011 09:01:20 Mehrdad wrote:
 On 9/26/2011 8:13 AM, Trass3r wrote:
 Your opCall isn't static.

Yeah, I don't want it to be. I want to use opCall on an instance, not on the type.

Well, it looks like having declared a non-static opCall makes the automatically generated constructor for the struct unusable - which may or may not be a bug (there are similar issues with declaring opCast). If you just declare a constructor, it should solve the problem. - Jonathan M Davis
Sep 26 2011
prev sibling next sibling parent "Regan Heath" <regan netmail.co.nz> writes:
On Mon, 26 Sep 2011 17:14:45 +0100, Jonathan M Davis <jmdavisProg gmx.com>  
wrote:

 On Monday, September 26, 2011 09:01:20 Mehrdad wrote:
 On 9/26/2011 8:13 AM, Trass3r wrote:
 Your opCall isn't static.

Yeah, I don't want it to be. I want to use opCall on an instance, not on the type.

Well, it looks like having declared a non-static opCall makes the automatically generated constructor for the struct unusable - which may or may not be a bug (there are similar issues with declaring opCast). If you just declare a constructor, it should solve the problem.

I couldn't get that to work.. but this does: import std.stdio; struct Adder { int v; auto opCall(int x) { return x + v; } } auto adder(int v) { Adder a; a.v = v; return a; } void main() { auto a = adder(5); writefln("%s", a(5)); } -- Using Opera's revolutionary email client: http://www.opera.com/mail/
Sep 26 2011
prev sibling next sibling parent Trass3r <un known.com> writes:
 Well, it looks like having declared a non-static opCall makes the
 automatically generated constructor for the struct unusable - which may  
 or may not be a bug

I do think it's a bug. Only a static opCall should interfere with the constructor.
Sep 26 2011
prev sibling parent "Steven Schveighoffer" <schveiguy yahoo.com> writes:
On Mon, 26 Sep 2011 13:13:52 -0400, Mehrdad <wfunction hotmail.com> wrote:

 On 9/26/2011 9:40 AM, Trass3r wrote:
 Well, it looks like having declared a non-static opCall makes the
 automatically generated constructor for the struct unusable - which  
 may or may not be a bug

I do think it's a bug. Only a static opCall should interfere with the constructor.


Might want to add this case (as I think it is important, and doesn't yet seem to have an exact duplicate report) to bug 6036. Looks like Kenji is on the case, and he usually comes up with a pull request :) -Steve
Sep 26 2011