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.