Manually allocate delegate?
    Jack Applegame via Digitalmars-d-learn 
    digitalmars-d-learn at puremagic.com
       
    Sat Nov 21 00:31:35 PST 2015
    
    
  
On Sunday, 12 July 2015 at 08:38:01 UTC, Tofu Ninja wrote:
> Is it even possible?
You can use function instead delegate, and bind captured 
variables as struct:
http://dpaste.dzfl.pl/6e23bbcfe17f
auto bind(F: R function(ARGS), R, ARGS...)(F fn, ARGS args) @nogc 
{
	struct Functor {
		F fn;
		ARGS args;
		this(F fn_, ARGS args_) {
			fn = fn_;
			args = args_;
		}
		R opCall() { return fn(args); }
	}
	return Functor(fn, args);
}
import std.stdio;
auto foo(int x) {
    static int bar(ref int x){ return x++; }
    return bind(&bar, x);
}
void main() {
	auto f1 = foo(5);
	auto f2 = foo(10);
	writefln("%s, %s, %s", f1(), f1(), f1());
	writefln("%s, %s, %s", f2(), f2(), f2());
}
    
    
More information about the Digitalmars-d-learn
mailing list