Thread overview
foreach reference to struct variable
Feb 07, 2008
Prashant V
Feb 07, 2008
Denton Cockburn
Feb 08, 2008
Prashant V
Feb 08, 2008
Jesse Phillips
Feb 08, 2008
Don Clugston
February 07, 2008
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++.
February 07, 2008
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
February 08, 2008
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?
February 08, 2008
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?
February 08, 2008
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)++;
February 08, 2008
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).