How can I store delegates in array?

H. S. Teoh hsteoh at quickfur.ath.cx
Fri Nov 30 08:06:11 PST 2012


On Fri, Nov 30, 2012 at 04:57:44PM +0100, Chopin wrote:
> I tried the following:
> 
> import std.stdio;
> import std.regex;
> 
> bool lol(string name, string val)
> {
>     if (name == "lal")
>         if (val[0..3] == "2124")
>             return true;
>     return false;
> }
> 
> bool lal(string name, string val) { return false; }
> alias bool delegate(string, string) DgType;
> void main()
> {
>     struct Filter
>     {
> 
>         private DgType[] delFuncs; // array with functions :D
> 
> 
>         public void add(DgType filter_func) {
>             delFuncs ~= filter_func;
>         }
>     }
> 
>     auto x = Filter();
>     x.add(&lol);
>     x.add(&lal);
> 
>     writeln(x);
> }
> 
> Didn't work... just a bunch of errors...
> 
> t.d(28): Error: function t.main.Filter.add (bool delegate(string,
> string) filter_func) is not callable using argument types (bool
> function(string name, string v
> al))
[...]

Ah, I see the problem. What you want are *function pointers*, not
delegates. So do this instead:

	alias bool function(string,string) FuncType;
	struct Filter
	{
		private FuncType[] delFuncs;
		...
	}


T

-- 
Prosperity breeds contempt, and poverty breeds consent. -- Suck.com


More information about the Digitalmars-d-learn mailing list