www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 11934] New: Allow `ref` in `foreach` over range iff `front` returns by `ref`

https://d.puremagic.com/issues/show_bug.cgi?id=11934

           Summary: Allow `ref` in `foreach` over range iff `front`
                    returns by `ref`
           Product: D
           Version: D2
          Platform: All
        OS/Version: All
            Status: NEW
          Keywords: accepts-invalid
          Severity: normal
          Priority: P2
         Component: DMD
        AssignedTo: nobody puremagic.com
        ReportedBy: verylonglogin.reg gmail.com


--- Comment #0 from Denis Shelomovskij <verylonglogin.reg gmail.com> 2014-01-16
11:45:17 MSK ---
Test code:
---
struct S1
{
     property bool empty();
     property int front();
    void popFront();
}

struct S2
{
     property bool empty();
     property ref int front();
    void popFront();
}

void main()
{
    static assert( __traits(compiles, { foreach(    n; S1()) { } }));
    static assert(!__traits(compiles, { foreach(ref n; S1()) { } })); // fails
    static assert( __traits(compiles, { foreach(ref n; S2()) { } }));
}
---

When `front` doesn't return by `ref` it means returned value is a temporary and
its modification is nonsense. In such case `ref` in `foreach` should be
rejected.

-- 
Configure issuemail: https://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Jan 15 2014