writing iterators without code duplication. inout?

Christophe travert at phare.normalesup.org
Wed Dec 21 09:49:18 PST 2011

"pompei2" , dans le message (digitalmars.D.learn:31164), a écrit :
> This is what I have, which works but has severe code duplication. 
> I hoped inout would help me here, but I just can't figure it out. 
> I also gave a try to ranges, but same thing again: I can only get 
> it to work if I define my things twice.

It's not optimal, and there is an ugly cast, but maybe this is a 
suitable workarrond for you :

  int delegate(int delegate(ref int)) doIter() const
      return (int delegate(ref int) dg)
          cast(typeof(this))(this).doIter()((ref int i) { int copy = i; dg(copy); });

  int delegate(ref inout int) opApply() inout;
  int delegate(int delegate(ref inout int)) doIter() inout;
are made to work. (I actually don't know if there is any obstacles to do 

More information about the Digitalmars-d-learn mailing list