| Thread overview | ||||||||
|---|---|---|---|---|---|---|---|---|
|
February 07, 2008 foreach reference to struct variable | ||||
|---|---|---|---|---|
| ||||
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 Re: foreach reference to struct variable | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Prashant V | 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 Re: foreach reference to struct variable | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Denton Cockburn | 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 Re: foreach reference to struct variable | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Prashant V | 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 Re: foreach reference to struct variable | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Prashant V | 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 Re: foreach reference to struct variable | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Prashant V | 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).
| |||
Copyright © 1999-2021 by the D Language Foundation
Permalink
Reply