[Issue 2443] New: opApply should allow delegates that are not ref if it makes no sense

d-bugmail at puremagic.com d-bugmail at puremagic.com
Thu Nov 6 07:44:45 PST 2008


http://d.puremagic.com/issues/show_bug.cgi?id=2443

           Summary: opApply should allow delegates that are not ref if it
                    makes no sense
           Product: D
           Version: 2.019
          Platform: PC
        OS/Version: Linux
            Status: NEW
          Severity: normal
          Priority: P2
         Component: DMD
        AssignedTo: bugzilla at digitalmars.com
        ReportedBy: schveiguy at yahoo.com


Currently, builtin arrays enjoy the restriction that you cannot ref the index
parameter when doing a foreach:

int main(string[] args)
{
   foreach(ref i, ref s; args) // Error: foreach: key cannot be out or ref
   { }
}

But when trying to do something similar on a struct, the compiler cannot
resolve the opApply:

struct S
{
    int[] arr;
    int opApply(int delegate(int i, ref int v) dg)
    {
        int result = 0;
        foreach(i, ref x; arr)
        {
            if((result = dg(i, x)) != 0)
                break;
        }
        return result;
    }
}

void main()
{
    S s;
    foreach(i, ref v; s)
    {}
}

emits:
testit.d(32): function testit.S.opApply (int delegate(int i, ref int v) dg)
does not match parameter types (int delegate(ref int __applyArg0, ref int v))
testit.d(32): Error: cannot implicitly convert expression (__foreachbody1) of
type int delegate(ref int __applyArg0, ref int v) to int delegate(int i, ref
int v)

This has implications for const as well, because it would make sense in S above
to have an opApply WITHOUT a ref int for the value in the case of a const S.


-- 



More information about the Digitalmars-d-bugs mailing list