www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Finding out about D - 102

reply Steve Teale <steve.teale britseyeview.com> writes:
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
next sibling parent reply Ary Borenszweig <ary esperanto.org.ar> writes:
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
parent Steve Teale <steve.teale britseyeview.com> writes:
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.
May 11 2009
prev sibling parent Jarrett Billingsley <jarrett.billingsley gmail.com> writes:
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