Thread overview | ||||||
---|---|---|---|---|---|---|
|
May 11, 2009 Finding out about D - 102 | ||||
---|---|---|---|---|
| ||||
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 Re: Finding out about D - 102 | ||||
---|---|---|---|---|
| ||||
Posted in reply to Steve Teale | 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 Re: Finding out about D - 102 | ||||
---|---|---|---|---|
| ||||
Posted in reply to Steve Teale | 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 realize that
>
> A ntl = 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 value.
|
May 12, 2009 Re: Finding out about D - 102 | ||||
---|---|---|---|---|
| ||||
Posted in reply to Ary Borenszweig | Ary Borenszweig Wrote:
> 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;
> }
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.
|
Copyright © 1999-2021 by the D Language Foundation