On Friday, 8 November 2013 at 13:14:33 UTC, Timon Gehr wrote:
On 11/08/2013 01:43 PM, Colin Grogan wrote:
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]

It is not even in scope here.

    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!

import std.stdio;
void main(){
Column!string col1 = new Column!string((ref m)=>"Hello, ", "test"); Column!string col2 = new Column!string((ref m)=>m.vars[0], "World");
    writef("%s", col1.nextValue);
    writefln("%s", col2.nextValue);
}

public class Column(Vars...){
    struct Members{ Vars vars; }
    private Members members;
    alias members this;
    string delegate(ref Members) func;

    this(string delegate(ref Members) func, Vars vars){
        this.vars = vars;
        this.func = func;
    }

    string nextValue(){
        return func(members);
    }
}


Ah, brilliant! I like that construct.
Thank you!

Reply via email to