www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 2411] New: Reference Tuple Foreach

reply d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=2411

           Summary: Reference Tuple Foreach
           Product: D
           Version: 2.019
          Platform: PC
        OS/Version: Windows
            Status: NEW
          Severity: enhancement
          Priority: P2
         Component: DMD
        AssignedTo: bugzilla digitalmars.com
        ReportedBy: dsimcha yahoo.com


Sometimes, when iterating over a tuple representation of a class or struct, I
want to modify the class/struct contents.  Ex:

struct S {
    uint foo = 1;
}

void main() {
    S s;
    foreach(element; s.tupleof)
        element = 2;
    writeln(s.foo); //Still prints 1
}

It would be nice if I could do something like:

foreach(ref element; s.tupleof)

and this would modify s.foo.


-- 
Oct 09 2008
next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=2411


David Simcha <dsimcha yahoo.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |sean invisibleduck.org




--- Comment #1 from David Simcha <dsimcha yahoo.com>  2009-06-02 05:51:21 PDT
---
*** Issue 3045 has been marked as a duplicate of this issue. ***

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Jun 02 2009
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=2411


bearophile_hugs eml.cc changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |bearophile_hugs eml.cc


--- Comment #2 from bearophile_hugs eml.cc 2010-03-07 10:28:09 PST ---
Bug 3835 is a partial duplicate of this (one case seems different).

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Mar 07 2010
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=2411


Trass3r <mrmocool gmx.de> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |mrmocool gmx.de


--- Comment #3 from Trass3r <mrmocool gmx.de> 2010-05-20 15:12:01 PDT ---
This is a workaround:

foreach (i, dummy ; s.tupleof)
        s.tupleof[i] = 2;

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
May 20 2010
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=2411


Kenji Hara <k.hara.pg gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Keywords|                            |patch


--- Comment #4 from Kenji Hara <k.hara.pg gmail.com> 2012-01-01 08:28:16 PST ---
https://github.com/D-Programming-Language/dmd/pull/596

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Jan 01 2012
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=2411



--- Comment #5 from Kenji Hara <k.hara.pg gmail.com> 2012-01-01 08:31:04 PST ---
My patch requires explicit 'ref'.

void main() {
    S s;
//  foreach(    element; s.tupleof)  // doesn't work
    foreach(ref element; s.tupleof)  // OK
        element = 2;
    assert(s.foo == 2);
}

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Jan 01 2012
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=2411



--- Comment #6 from David Simcha <dsimcha yahoo.com> 2012-01-01 13:44:46 PST ---
(In reply to comment #5)
 My patch requires explicit 'ref'.
 
 void main() {
     S s;
 //  foreach(    element; s.tupleof)  // doesn't work
     foreach(ref element; s.tupleof)  // OK
         element = 2;
     assert(s.foo == 2);
(In reply to comment #5)
 My patch requires explicit 'ref'.
 
 void main() {
     S s;
 //  foreach(    element; s.tupleof)  // doesn't work
     foreach(ref element; s.tupleof)  // OK
         element = 2;
     assert(s.foo == 2);
 }
It seems to me like the only logical way to do this is to require explicit ref. The semantics should be the same as foreach over ranges. Nice work. -- Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email ------- You are receiving this mail because: -------
Jan 01 2012
prev sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=2411


Walter Bright <bugzilla digitalmars.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |RESOLVED
                 CC|                            |bugzilla digitalmars.com
         Resolution|                            |FIXED


--- Comment #7 from Walter Bright <bugzilla digitalmars.com> 2012-01-10
12:25:30 PST ---
https://github.com/D-Programming-Language/dmd/commit/d3294a136cf6355442dc4e897e46334655f92f15

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Jan 10 2012