D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 13370 - Allow @nogc delegates in foreach
Summary: Allow @nogc delegates in foreach
Status: RESOLVED INVALID
Alias: None
Product: D
Classification: Unclassified
Component: dmd (show other issues)
Version: D2
Hardware: All All
: P1 enhancement
Assignee: No Owner
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2014-08-24 16:19 UTC by Stefan Frijters
Modified: 2020-03-21 03:56 UTC (History)
1 user (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description Stefan Frijters 2014-08-24 16:19:27 UTC
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
Comment 1 Ketmar Dark 2014-08-24 18:59:35 UTC
seems that this request is a child of misunderstanding. delegate body in this case includes 'write()', which is not @nogc. if we'll remove write(), dmd will correctly infer @nogc attribute.