Foreach/opApply with @nogc

Stefan Frijters via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Sun Aug 24 06:22:49 PDT 2014


Should this be possible? I admit to not being fully clear on the 
way delegates are handled, but maybe someone can shed some light? 
As an example I use a snippet Ali uses to demonstrate opApply:

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 I compile this with 2.066.0 I get

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 doesn't actually complain about anything gc, just about the 
signature, so I was wondering if there is some restriction inside 
the foreach implementation that can be lifted? Thanks in advance 
for any hints!


More information about the Digitalmars-d-learn mailing list