Thread overview | |||||||
---|---|---|---|---|---|---|---|
|
November 30, 2012 How can I store delegates in array? | ||||
---|---|---|---|---|
| ||||
Hi! I've never used delegates before etc. not really familiar with it. I was trying something like this: 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; } void main() { struct Filter { private bool delegate(string, string)[] delFuncs; // array with functions :D public void add(bool delegate(string, string) filter_func) { delFuncs ~= filter_func; } } auto x = Filter(); x.add(&lol); x.add(&lal); writeln(x); } How can I make this work? Thank you :) |
November 30, 2012 Re: How can I store delegates in array? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Chopin | On Fri, Nov 30, 2012 at 04:41:40PM +0100, Chopin wrote: > Hi! > > I've never used delegates before etc. not really familiar with it. [...] You could try something like this: alias bool delegate(string, string) DgType; DgType[] arrayOfDelegates; T -- Why waste time learning, when ignorance is instantaneous? -- Hobbes, from Calvin & Hobbes |
November 30, 2012 Re: How can I store delegates in array? | ||||
---|---|---|---|---|
| ||||
Posted in reply to H. S. Teoh | 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)) t.d(28): Error: cannot implicitly convert expression (& lol) of type bool function(string name, string val) to bool delegate(string, string) t.d(29): 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)) t.d(29): Error: cannot implicitly convert expression (& lal) of type bool function(string name, string val) to bool delegate(string, string) Thanks for help! |
November 30, 2012 Re: How can I store delegates in array? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Chopin | 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 |
November 30, 2012 Re: How can I store delegates in array? | ||||
---|---|---|---|---|
| ||||
Posted in reply to H. S. Teoh | Amazing! It works! Thank you so much :) |
Copyright © 1999-2021 by the D Language Foundation