www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 13370] New: Allow nogc delegates in foreach

https://issues.dlang.org/show_bug.cgi?id=13370

          Issue ID: 13370
           Summary: Allow  nogc delegates in foreach
           Product: D
           Version: D2
          Hardware: All
                OS: All
            Status: NEW
          Severity: enhancement
          Priority: P1
         Component: DMD
          Assignee: nobody puremagic.com
          Reporter: sfrijters gmail.com

Currently, an opApply function cannot be  nogc:

struct NumberRange {
  int begin;
  int end;

  int opApply(int delegate(ref int)  nogc operations)  nogc const 
{
    int result = 0;

    for (int number = begin; number != end; ++number) {
      result = operations(number);

      if (result) {
        break;
      }
    }
    return result;
  }
}

void main() {
  import std.stdio;
  foreach (element; NumberRange(3, 7)) { // line 21
    write(element, ' ');
  }
}

When compiled with 2.066.0:

opapply.d(21): Error: function opapply.NumberRange.opApply (int 
delegate(ref int)  nogc operations) const is not callable using 
argument types (int delegate(ref int __applyArg0)  system)

It would be nice if this code could be allowed, preferably by inference,
otherwise by allowing something like

foreach (element; NumberRange(3, 7)  nogc) { ... }

Discussion here:
http://forum.dlang.org/thread/fqaskxirvcxrdevmacdo forum.dlang.org

--
Aug 24 2014