Capturing a variable by value?

Basile B. via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Wed Feb 3 11:41:45 PST 2016


On Wednesday, 3 February 2016 at 18:09:52 UTC, ZombineDev wrote:
> I think these two links, more or less, answer my question:
> http://stackoverflow.com/questions/29759419/closures-in-loops-capturing-by-reference
> https://issues.dlang.org/show_bug.cgi?id=2043

Another approach:

import std.range, std.array, std.stdio;

struct InputRangeHook(T, alias Fun)
if (isInputRange!T)
{
     private T _t;
     this(T t)
     {
         _t = t;
     }
     void popFront()
     {
         Fun(front());
         _t.popFront;
     }
     bool empty()
     {
         return _t.empty();
     }
     auto front()
     {
         return _t.front;
     }
}

void main()
{
     foreach(i; InputRangeHook!(int[],(a) {write(a," 
");})(iota(0,10).array)){}
}


More information about the Digitalmars-d-learn mailing list