Bug 13370 – Allow @nogc delegates in foreach

Status
RESOLVED
Resolution
INVALID
Severity
enhancement
Priority
P1
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2014-08-24T16:19:27Z
Last change time
2020-03-21T03:56:37Z
Assigned to
No Owner
Creator
Stefan Frijters

Comments

Comment #0 by sfrijters — 2014-08-24T16:19:27Z
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/[email protected]
Comment #1 by ketmar — 2014-08-24T18:59:35Z
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.