www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 4707] New: auto ref for foreach loops

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

           Summary: auto ref for foreach loops
           Product: D
           Version: D2
          Platform: Other
        OS/Version: Windows
            Status: NEW
          Severity: enhancement
          Priority: P2
         Component: DMD
        AssignedTo: nobody puremagic.com
        ReportedBy: dsimcha yahoo.com



The following should be possible, with x being ref if possible and non-ref
otherwise.  I've toyed with the possibility of adding opApply support to some
stuff in std.range and std.algorithm and this is a blocker for doing it at all
properly.

void main() {
    foreach(auto ref x; [1, 2]) {}
}

test9.d(2): basic type expected, not auto
test9.d(2): no identifier for declarator int
test9.d(2): found 'auto' when expecting ';'
test9.d(2): found ';' when expecting ')'
test9.d(2): found ')' when expecting ';' following statement

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


nfxjfg gmail.com changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |nfxjfg gmail.com



How would this be different from the following (which works)?

void main() {
    foreach(ref x; [1, 2]) {}
}

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




Because if the range didn't support ref iteration, the foreach loop would work
with non-ref iteration rather than producing a compile time error.(In reply to

 How would this be different from the following (which works)?
 
 void main() {
     foreach(ref x; [1, 2]) {}
 }
If the range didn't support ref iteration, the foreach loop would work with non-ref iteration rather than producing a compile time error. -- Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email ------- You are receiving this mail because: -------
Aug 21 2010
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=4707




Another use case for this is if you are iterating over something that may be
expensive to copy.  In these cases, doing foreach(elem; stuff) is inefficient
because it produces an unnecessary copy, but foreach(ref elem; stuff) isn't
generic enough because it won't work on ranges w/o lvalue elements.

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


Martin Nowak <code dawg.eu> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |code dawg.eu



This is still an interesting proposal.

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


monarchdodra gmail.com changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |monarchdodra gmail.com




 This is still an interesting proposal.
Seconded. Thoguh I do seem to remember that another Bug Report about this was open... I did find this interesting thread about it though: http://forum.dlang.org/thread/ceftaiklanejfhodbpix forum.dlang.org This proposal has 2 very important (IMO) ramifications: First, it would allow making "foreach(ref; range)" illegal, if said range doesn't give ref access. This is a *big* source of bugs. Second, once we have this, we would be able to write *correct* generic code. Read this extract from "reduce": // For now, just iterate using ref to avoid unnecessary copying. // When Bug 2443 is fixed, this may need to change. foreach (ref elem; r) { Or "I'm doing wrong, but it seems to work". So long story short, if we ever hope to have "ref-correct" foreach, we *must* have "auto ref" to make foreach useable in generic code. This gets a vote from me -- Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email ------- You are receiving this mail because: -------
Nov 02 2013