digitalmars.D.learn - Finding out about D - 102
- Steve Teale (43/43) May 11 2009 OK, so structs are a different beast in D than they are in C++. This res...
- Ary Borenszweig (5/19) May 11 2009 with(nameTooLong[whatever.whatever]) {
- Steve Teale (4/24) May 11 2009 Ary,
- Jarrett Billingsley (6/9) May 11 2009 ealize that
OK, so structs are a different beast in D than they are in C++. This results in one of my most common pitfalls. I'll find myself writing: struct A { int a; int b; } A[] nameTooLong = ...; foreach (whatever; thingie) { nameTooLong[whatever.whatever].a = whatever.x*3; nameTooLong[whatever.whatever].b = whatever.y/3; // more of the same sort of stuff } So I get fed up typing 'nameTooLong[whatever.whatever]', and instead I write foreach (whatever; thingie) { A ntl = nameTooLong[whatever.whatever]; ntl.a = whatever.x*3; ntl.b = whatever.y/3; // more of the same sort of stuff } Then I chase a bug in my program, which compiled OK. After some time, I realize that A ntl = nameTooLong[whatever.whatever]; is doing a copy, which is not what I was thinking about at all - old C++ habits. ntl = ...; has no effect whatsoever on nameTooLong[whatever.whatever]. So then - pissed off by that point - I rewrite it as: foreach (whatever; thingie) { A* ntl = &nameTooLong[whatever.whatever]; // This suggests an ambiguity in the language? ntl.a = whatever.x*3; ntl.b = whatever.y/3; // more of the same sort of stuff } This works OK, but it's still not the D way to do things. Try: foreach (whatever; thingie) { alias nameTooLong[whatever.whatever] ntl; ntl.a = whatever.x*3; ntl.b = whatever.y/3; // more of yer same sort of stuff }
May 11 2009
Steve Teale wrote:OK, so structs are a different beast in D than they are in C++. This results in one of my most common pitfalls. I'll find myself writing: struct A { int a; int b; } A[] nameTooLong = ...; foreach (whatever; thingie) { nameTooLong[whatever.whatever].a = whatever.x*3; nameTooLong[whatever.whatever].b = whatever.y/3;with(nameTooLong[whatever.whatever]) { a = whatever.x*3; b = whatever.y/3; }
May 11 2009
Ary Borenszweig Wrote:Steve Teale wrote:Ary, Yes I use with quite often, it's when I have two of the beasts where I want to use with at the same time that I have fallen into this. Now that I've rubbed my nose in it I'm sure I won't do it again.OK, so structs are a different beast in D than they are in C++. This results in one of my most common pitfalls. I'll find myself writing: struct A { int a; int b; } A[] nameTooLong = ...; foreach (whatever; thingie) { nameTooLong[whatever.whatever].a = whatever.x*3; nameTooLong[whatever.whatever].b = whatever.y/3;with(nameTooLong[whatever.whatever]) { a = whatever.x*3; b = whatever.y/3; }
May 11 2009
On Mon, May 11, 2009 at 1:48 PM, Steve Teale <steve.teale britseyeview.com> wrote:Then I chase a bug in my program, which compiled OK. After some time, I r=ealize that=A0 A ntl =3D nameTooLong[whatever.whatever]; is doing a copy, which is not what I was thinking about at all - old C++ =habits. Um, C++ works exactly the same way, if you're using classes/structs by valu= e.
May 11 2009