digitalmars.D - Difference between static and non-static opAssign?
- =?ISO-8859-1?Q?Alex_R=F8nne_Petersen?= (7/31) Sep 01 2011 And:
- Steven Schveighoffer (30/62) Sep 01 2011 =
- =?UTF-8?B?QWxleCBSw7hubmUgUGV0ZXJzZW4=?= (4/67) Sep 01 2011 So that's to say x = 5; with a static opAssign would actually just... do...
- Timon Gehr (3/77) Sep 01 2011 It will execute the body of static opAssign which could contain
- =?UTF-8?B?QWxleCBSw7hubmUgUGV0ZXJzZW4=?= (3/82) Sep 01 2011 Oh right. Hadn't thought of that. It still seems very obscure, though.
- Steven Schveighoffer (16/24) Sep 01 2011 for
- =?UTF-8?B?QWxleCBSw7hubmUgUGV0ZXJzZW4=?= (4/22) Sep 01 2011 I agree, it is confusing. Many other languages get by just fine without
- Timon Gehr (5/38) Sep 01 2011 s = 1;
- =?ISO-8859-1?Q?Alex_R=F8nne_Petersen?= (5/38) Sep 01 2011 Somewhat related question: should opAssign return void or return an
- Timon Gehr (3/47) Sep 01 2011 It does matter for eg multiple assignment:
- =?ISO-8859-1?Q?Alex_R=F8nne_Petersen?= (3/52) Sep 01 2011 Ah, that makes sense! Thanks.
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
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
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:So that's to say x = 5; with a static opAssign would actually just... do nothing? - AlexHi, Consider this: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. -Stevestruct 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?
Sep 01 2011
On 09/01/2011 09:18 PM, Alex Rønne Petersen wrote:On 01-09-2011 21:13, Steven Schveighoffer wrote:It will execute the body of static opAssign which could contain arbitrary code.On Thu, 01 Sep 2011 15:08:29 -0400, Alex Rønne Petersen <xtzgzorex gmail.com> wrote:So that's to say x = 5; with a static opAssign would actually just... do nothing? - AlexHi, Consider this: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. -Stevestruct 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?
Sep 01 2011
On 01-09-2011 21:26, Timon Gehr wrote:On 09/01/2011 09:18 PM, Alex Rønne Petersen wrote:Oh right. Hadn't thought of that. It still seems very obscure, though. - AlexOn 01-09-2011 21:13, Steven Schveighoffer wrote:It will execute the body of static opAssign which could contain arbitrary code.On Thu, 01 Sep 2011 15:08:29 -0400, Alex Rønne Petersen <xtzgzorex gmail.com> wrote:So that's to say x = 5; with a static opAssign would actually just... do nothing? - AlexHi, Consider this: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. -Stevestruct 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?
Sep 01 2011
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:forSince you are allowed to call static member functions, s.opAssign(1) =s,the static one succeeds, but some of us are pushing to make this invalid. It makes things very confusing when you call static function=. do =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..=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
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:I agree, it is confusing. Many other languages get by just fine without static calls on instances, so I'm sure D can too... - AlexOn 01-09-2011 21:13, Steven Schveighoffer wrote: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 -SteveSince 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?
Sep 01 2011
On 09/01/2011 09:08 PM, Alex Rønne Petersen wrote:Hi, Consider this:s = 1; is rewritten to s.opAssign(1); Inside non-static opAssign, therefore 'this' will refer to s.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
On 01-09-2011 21:08, Alex Rønne Petersen wrote:Hi, Consider this: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... - Alexstruct 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
On 09/01/2011 09:25 PM, Alex Rønne Petersen wrote:On 01-09-2011 21:08, Alex Rønne Petersen wrote:It does matter for eg multiple assignment: a = b = c; // return void and this will not work anymoreHi, Consider this: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... - Alexstruct 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
On 01-09-2011 21:27, Timon Gehr wrote:On 09/01/2011 09:25 PM, Alex Rønne Petersen wrote:Ah, that makes sense! Thanks. - AlexOn 01-09-2011 21:08, Alex Rønne Petersen wrote:It does matter for eg multiple assignment: a = b = c; // return void and this will not work anymoreHi, Consider this: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... - Alexstruct 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