Delegate function access to classes local variable

Colin Grogan grogan.colin at gmail.com
Fri Nov 8 04:43:35 PST 2013


Hi folks,

I'm having some issue getting a delegate function access to a 
classes member variable.

At object construct time, I'm passing in a delegate function, and 
a list of parameters after.
The parameters are saved to a variable called vars.
Should I then not be able to access that vars variable from 
inside my delegate function?

I guess some code is a better explanation:
import std.stdio;
void main()
{
	Column!string col1 = new Column!string( {return "test"; }, 
"Hello, ");
	Column!string col2 = new Column!string( {return vars[0]; }, 
"World"); // Compilation fail!! Delegate cant see vars[0]
	writef("%s", col1.nextValue);
	writefln("%s", col2.nextValue);
// I want it to print "Hello, World" here
}

public class Column(Vars...){
	private Vars vars;
	public string delegate() func;
	
	public this(string delegate() func, Vars vars){
		this.vars = vars;
		this.func = func;
	}
	
	public string nextValue(){
		return this.func();
	}
}


The compilation error is:
source/app.d(5): Error: undefined identifier vars

This has been wrecking my head for a couple days now, I'm half 
way resigned to the fact it cant work but said I'd ask here to be 
sure.

Thanks!


More information about the Digitalmars-d-learn mailing list