How would you do this in D?

Daniel Gibson metalcaedes at gmail.com
Tue May 10 18:05:35 PDT 2011


Am 11.05.2011 02:56, schrieb Daniel Gibson:
> 
> There may be a more clever template-trick, however.

What about:

void EVERY(int threshold, alias action, string file = __FILE__, int line
= __LINE__)()
{
  static int cnt;
  ++cnt;
  if(cnt==threshold) {
    action();
    cnt=0;
  }
}

void main() {
  int i,j;
  while(i<10) {
    EVERY!(10, {++j;} )();
    EVERY!(2, {++i;} ); // last () was optional
  }
  assert(j==2);
  assert(i==10);
}

:-)


More information about the Digitalmars-d mailing list