www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Difference between static and non-static opAssign?

reply =?ISO-8859-1?Q?Alex_R=F8nne_Petersen?= <xtzgzorex gmail.com> writes:
Hi,

Consider this:

 struct S
 {
     static S opAssign(int rhs)
     {
         return S();
     }
 }

 void main()
 {
     S s;
     s = 1;
 }
And:
 struct S
 {
     S opAssign(int rhs)
     {
         return S();
     }
 }

 void main()
 {
     S s;
     s = 1;
 }
Both will compile. I don't get it. I can understand it in the case of the static opAssign, but with the non-static one, what does 'this' refer to inside opAssign? And why are both seemingly allowed? - Alex
Sep 01 2011
next sibling parent reply "Steven Schveighoffer" <schveiguy yahoo.com> writes:
On Thu, 01 Sep 2011 15:08:29 -0400, Alex R=C3=B8nne Petersen  =

<xtzgzorex gmail.com> wrote:

 Hi,

 Consider this:

 struct S
 {
     static S opAssign(int rhs)
     {
         return S();
     }
 }

 void main()
 {
     S s;
     s =3D 1;
 }
And:
 struct S
 {
     S opAssign(int rhs)
     {
         return S();
     }
 }

 void main()
 {
     S s;
     s =3D 1;
 }
Both will compile. I don't get it. I can understand it in the case of =
=
 the static opAssign, but with the non-static one, what does 'this' ref=
er =
 to inside opAssign? And why are both seemingly allowed?
s =3D 1 is rewritten as s.opAssign(1). So in the non-static version, 't= his' = refers to the struct you called the function with (in your example, s). so: struct S { int x; S opAssign(int rhs) { x =3D rhs; return this; } } void main() { S s; s =3D 5; assert(s.x =3D=3D 5); } Since you are allowed to call static member functions, s.opAssign(1) for= = the static one succeeds, but some of us are pushing to make this invalid= . = It makes things very confusing when you call static functions, but it = looks like you are calling non-static member functions. -Steve
Sep 01 2011
parent reply =?UTF-8?B?QWxleCBSw7hubmUgUGV0ZXJzZW4=?= <xtzgzorex gmail.com> writes:
On 01-09-2011 21:13, Steven Schveighoffer wrote:
 On Thu, 01 Sep 2011 15:08:29 -0400, Alex Rønne Petersen
 <xtzgzorex gmail.com> wrote:

 Hi,

 Consider this:

 struct S
 {
 static S opAssign(int rhs)
 {
 return S();
 }
 }

 void main()
 {
 S s;
 s = 1;
 }
And:
 struct S
 {
 S opAssign(int rhs)
 {
 return S();
 }
 }

 void main()
 {
 S s;
 s = 1;
 }
Both will compile. I don't get it. I can understand it in the case of the static opAssign, but with the non-static one, what does 'this' refer to inside opAssign? And why are both seemingly allowed?
s = 1 is rewritten as s.opAssign(1). So in the non-static version, 'this' refers to the struct you called the function with (in your example, s). so: struct S { int x; S opAssign(int rhs) { x = rhs; return this; } } void main() { S s; s = 5; assert(s.x == 5); } Since you are allowed to call static member functions, s.opAssign(1) for the static one succeeds, but some of us are pushing to make this invalid. It makes things very confusing when you call static functions, but it looks like you are calling non-static member functions. -Steve
So that's to say x = 5; with a static opAssign would actually just... do nothing? - Alex
Sep 01 2011
next sibling parent reply Timon Gehr <timon.gehr gmx.ch> writes:
On 09/01/2011 09:18 PM, Alex Rønne Petersen wrote:
 On 01-09-2011 21:13, Steven Schveighoffer wrote:
 On Thu, 01 Sep 2011 15:08:29 -0400, Alex Rønne Petersen
 <xtzgzorex gmail.com> wrote:

 Hi,

 Consider this:

 struct S
 {
 static S opAssign(int rhs)
 {
 return S();
 }
 }

 void main()
 {
 S s;
 s = 1;
 }
And:
 struct S
 {
 S opAssign(int rhs)
 {
 return S();
 }
 }

 void main()
 {
 S s;
 s = 1;
 }
Both will compile. I don't get it. I can understand it in the case of the static opAssign, but with the non-static one, what does 'this' refer to inside opAssign? And why are both seemingly allowed?
s = 1 is rewritten as s.opAssign(1). So in the non-static version, 'this' refers to the struct you called the function with (in your example, s). so: struct S { int x; S opAssign(int rhs) { x = rhs; return this; } } void main() { S s; s = 5; assert(s.x == 5); } Since you are allowed to call static member functions, s.opAssign(1) for the static one succeeds, but some of us are pushing to make this invalid. It makes things very confusing when you call static functions, but it looks like you are calling non-static member functions. -Steve
So that's to say x = 5; with a static opAssign would actually just... do nothing? - Alex
It will execute the body of static opAssign which could contain arbitrary code.
Sep 01 2011
parent =?UTF-8?B?QWxleCBSw7hubmUgUGV0ZXJzZW4=?= <xtzgzorex gmail.com> writes:
On 01-09-2011 21:26, Timon Gehr wrote:
 On 09/01/2011 09:18 PM, Alex Rønne Petersen wrote:
 On 01-09-2011 21:13, Steven Schveighoffer wrote:
 On Thu, 01 Sep 2011 15:08:29 -0400, Alex Rønne Petersen
 <xtzgzorex gmail.com> wrote:

 Hi,

 Consider this:

 struct S
 {
 static S opAssign(int rhs)
 {
 return S();
 }
 }

 void main()
 {
 S s;
 s = 1;
 }
And:
 struct S
 {
 S opAssign(int rhs)
 {
 return S();
 }
 }

 void main()
 {
 S s;
 s = 1;
 }
Both will compile. I don't get it. I can understand it in the case of the static opAssign, but with the non-static one, what does 'this' refer to inside opAssign? And why are both seemingly allowed?
s = 1 is rewritten as s.opAssign(1). So in the non-static version, 'this' refers to the struct you called the function with (in your example, s). so: struct S { int x; S opAssign(int rhs) { x = rhs; return this; } } void main() { S s; s = 5; assert(s.x == 5); } Since you are allowed to call static member functions, s.opAssign(1) for the static one succeeds, but some of us are pushing to make this invalid. It makes things very confusing when you call static functions, but it looks like you are calling non-static member functions. -Steve
So that's to say x = 5; with a static opAssign would actually just... do nothing? - Alex
It will execute the body of static opAssign which could contain arbitrary code.
Oh right. Hadn't thought of that. It still seems very obscure, though. - Alex
Sep 01 2011
prev sibling parent reply "Steven Schveighoffer" <schveiguy yahoo.com> writes:
On Thu, 01 Sep 2011 15:18:58 -0400, Alex R=C3=B8nne Petersen  =

<xtzgzorex gmail.com> wrote:

 On 01-09-2011 21:13, Steven Schveighoffer wrote:
 Since you are allowed to call static member functions, s.opAssign(1) =
for
 the static one succeeds, but some of us are pushing to make this
 invalid. It makes things very confusing when you call static function=
s,
 but it looks like you are calling non-static member functions.
So that's to say x =3D 5; with a static opAssign would actually just..=
. do =
 nothing?
Well, it would do nothing to x, since x isn't passed to the function (it= = could possibly affect some static member). Which is why I feel it's a f= ar = more confusing feature than it is worth. However, there are some legitimate reasons to have a static function be = = callable using an instance, which have to do with generic programming. I recently created an enhancement request on this: http://d.puremagic.com/issues/show_bug.cgi?id=3D6579 -Steve
Sep 01 2011
parent =?UTF-8?B?QWxleCBSw7hubmUgUGV0ZXJzZW4=?= <xtzgzorex gmail.com> writes:
On 01-09-2011 21:29, Steven Schveighoffer wrote:
 On Thu, 01 Sep 2011 15:18:58 -0400, Alex Rønne Petersen
 <xtzgzorex gmail.com> wrote:

 On 01-09-2011 21:13, Steven Schveighoffer wrote:
 Since you are allowed to call static member functions, s.opAssign(1) for
 the static one succeeds, but some of us are pushing to make this
 invalid. It makes things very confusing when you call static functions,
 but it looks like you are calling non-static member functions.
So that's to say x = 5; with a static opAssign would actually just... do nothing?
Well, it would do nothing to x, since x isn't passed to the function (it could possibly affect some static member). Which is why I feel it's a far more confusing feature than it is worth. However, there are some legitimate reasons to have a static function be callable using an instance, which have to do with generic programming. I recently created an enhancement request on this: http://d.puremagic.com/issues/show_bug.cgi?id=6579 -Steve
I agree, it is confusing. Many other languages get by just fine without static calls on instances, so I'm sure D can too... - Alex
Sep 01 2011
prev sibling next sibling parent Timon Gehr <timon.gehr gmx.ch> writes:
On 09/01/2011 09:08 PM, Alex Rønne Petersen wrote:
 Hi,

 Consider this:

 struct S
 {
 static S opAssign(int rhs)
 {
 return S();
 }
 }

 void main()
 {
 S s;
 s = 1;
 }
And:
 struct S
 {
 S opAssign(int rhs)
 {
 return S();
 }
 }

 void main()
 {
 S s;
 s = 1;
 }
Both will compile. I don't get it. I can understand it in the case of the static opAssign, but with the non-static one, what does 'this' refer to inside opAssign? And why are both seemingly allowed? - Alex
s = 1; is rewritten to s.opAssign(1); Inside non-static opAssign, therefore 'this' will refer to s.
Sep 01 2011
prev sibling parent reply =?ISO-8859-1?Q?Alex_R=F8nne_Petersen?= <xtzgzorex gmail.com> writes:
On 01-09-2011 21:08, Alex Rønne Petersen wrote:
 Hi,

 Consider this:

 struct S
 {
 static S opAssign(int rhs)
 {
 return S();
 }
 }

 void main()
 {
 S s;
 s = 1;
 }
And:
 struct S
 {
 S opAssign(int rhs)
 {
 return S();
 }
 }

 void main()
 {
 S s;
 s = 1;
 }
Both will compile. I don't get it. I can understand it in the case of the static opAssign, but with the non-static one, what does 'this' refer to inside opAssign? And why are both seemingly allowed? - Alex
Somewhat related question: should opAssign return void or return an actual value? Does it matter? If x = y is rewritten to x.opAssign(y), I assume it doesn't... - Alex
Sep 01 2011
parent reply Timon Gehr <timon.gehr gmx.ch> writes:
On 09/01/2011 09:25 PM, Alex Rønne Petersen wrote:
 On 01-09-2011 21:08, Alex Rønne Petersen wrote:
 Hi,

 Consider this:

 struct S
 {
 static S opAssign(int rhs)
 {
 return S();
 }
 }

 void main()
 {
 S s;
 s = 1;
 }
And:
 struct S
 {
 S opAssign(int rhs)
 {
 return S();
 }
 }

 void main()
 {
 S s;
 s = 1;
 }
Both will compile. I don't get it. I can understand it in the case of the static opAssign, but with the non-static one, what does 'this' refer to inside opAssign? And why are both seemingly allowed? - Alex
Somewhat related question: should opAssign return void or return an actual value? Does it matter? If x = y is rewritten to x.opAssign(y), I assume it doesn't... - Alex
It does matter for eg multiple assignment: a = b = c; // return void and this will not work anymore
Sep 01 2011
parent =?ISO-8859-1?Q?Alex_R=F8nne_Petersen?= <xtzgzorex gmail.com> writes:
On 01-09-2011 21:27, Timon Gehr wrote:
 On 09/01/2011 09:25 PM, Alex Rønne Petersen wrote:
 On 01-09-2011 21:08, Alex Rønne Petersen wrote:
 Hi,

 Consider this:

 struct S
 {
 static S opAssign(int rhs)
 {
 return S();
 }
 }

 void main()
 {
 S s;
 s = 1;
 }
And:
 struct S
 {
 S opAssign(int rhs)
 {
 return S();
 }
 }

 void main()
 {
 S s;
 s = 1;
 }
Both will compile. I don't get it. I can understand it in the case of the static opAssign, but with the non-static one, what does 'this' refer to inside opAssign? And why are both seemingly allowed? - Alex
Somewhat related question: should opAssign return void or return an actual value? Does it matter? If x = y is rewritten to x.opAssign(y), I assume it doesn't... - Alex
It does matter for eg multiple assignment: a = b = c; // return void and this will not work anymore
Ah, that makes sense! Thanks. - Alex
Sep 01 2011