www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - foreach reference to struct variable

reply Prashant V <dmd prashantv.com> writes:
I wanted to use D for a simple program just to get a feel for how to use it,
and while overall it was pretty easy, I found an annoying problem that I could
not find an answer to online.

I want to be able to iterate through an array, and I want the current element
to be the actual struct in the array itself. At the moment, it looks like the
foreach is making a copy of the struct in the array so any changes are not made
to the original in the array.

Is there any way to tell D to specifically give me the referenced variable
rather than a copy? I had to resort to using for with an index, and I don't
really think that is an ideal solution.

I want to know if there is a D syntax for the & reference modifier in C++.
Feb 07 2008
parent reply Denton Cockburn <diboss hotmail.com> writes:
Prashant V Wrote:

 I wanted to use D for a simple program just to get a feel for how to use it,
and while overall it was pretty easy, I found an annoying problem that I could
not find an answer to online.
 
 I want to be able to iterate through an array, and I want the current element
to be the actual struct in the array itself. At the moment, it looks like the
foreach is making a copy of the struct in the array so any changes are not made
to the original in the array.
 
 Is there any way to tell D to specifically give me the referenced variable
rather than a copy? I had to resort to using for with an index, and I don't
really think that is an ideal solution.
 
 I want to know if there is a D syntax for the & reference modifier in C++.

use ref while indexing: foreach (ref s; structs) // do something
Feb 07 2008
next sibling parent reply Prashant V <dmd prashantv.com> writes:
Denton Cockburn Wrote:
 use ref while indexing:
 
 foreach (ref s; structs)
    // do something

Ah that works great! Thanks. Is there anything similar for variables. Eg can I go int test = 5; ref auto test2 = test; test++; and have the change reflected in test?
Feb 07 2008
next sibling parent "Unknown W. Brackets" <unknown simplemachines.org> writes:
You can use the actual & operator...

	int a = 5;
	auto b = &a;

	writefln("a: %s, b: %s", a, *b);

And there's also this, but it's not exactly the same thing (it literally 
is just an aliased variable name):

	int a = 5;
	alias a b;

	writefln("a: %s, b: %s", a, b);

-[Unknown]


Prashant V wrote:
 Denton Cockburn Wrote:
 use ref while indexing:

 foreach (ref s; structs)
    // do something

Ah that works great! Thanks. Is there anything similar for variables. Eg can I go int test = 5; ref auto test2 = test; test++; and have the change reflected in test?

Feb 07 2008
prev sibling parent Don Clugston <dac nospam.com.au> writes:
Prashant V wrote:
 Denton Cockburn Wrote:
 use ref while indexing:

 foreach (ref s; structs)
    // do something

Ah that works great! Thanks. Is there anything similar for variables. Eg can I go int test = 5; ref auto test2 = test; test++; and have the change reflected in test?

Not at present, but IIRC it is an intended future feature (with syntax exactly as you've written).
Feb 08 2008
prev sibling parent Jesse Phillips <jessekphillips gmail.com> writes:
On Fri, 08 Feb 2008 00:21:01 -0500, Prashant V wrote:

 Denton Cockburn Wrote:
 use ref while indexing:
 
 foreach (ref s; structs)
    // do something

Ah that works great! Thanks. Is there anything similar for variables. Eg can I go int test = 5; ref auto test2 = test; test++; and have the change reflected in test?

You would have to make test2 a pointer. int test = 5 auto test2 = &test (*test2)++;
Feb 07 2008