digitalmars.D - foreach reference to struct variable
- Prashant V (4/4) Feb 07 2008 I wanted to use D for a simple program just to get a feel for how to use...
- Denton Cockburn (4/11) Feb 07 2008 use ref while indexing:
- Prashant V (6/10) Feb 07 2008 Ah that works great! Thanks. Is there anything similar for variables. Eg...
- Unknown W. Brackets (11/24) Feb 07 2008 You can use the actual & operator...
- Jesse Phillips (5/19) Feb 07 2008 You would have to make test2 a pointer.
- Don Clugston (3/16) Feb 08 2008 Not at present, but IIRC it is an intended future feature (with syntax e...
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
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
Denton Cockburn Wrote:use ref while indexing: foreach (ref s; structs) // do somethingAh 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
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 somethingAh 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
On Fri, 08 Feb 2008 00:21:01 -0500, Prashant V wrote:Denton Cockburn Wrote:You would have to make test2 a pointer. int test = 5 auto test2 = &test (*test2)++;use ref while indexing: foreach (ref s; structs) // do somethingAh 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
Prashant V wrote:Denton Cockburn Wrote:Not at present, but IIRC it is an intended future feature (with syntax exactly as you've written).use ref while indexing: foreach (ref s; structs) // do somethingAh 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 08 2008