www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - How can I store delegates in array?

reply "Chopin" <robert.bue gmail.com> writes:
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 :)
Nov 30 2012
parent reply "H. S. Teoh" <hsteoh quickfur.ath.cx> writes:
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
Nov 30 2012
parent reply "Chopin" <robert.bue gmail.com> writes:
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!
Nov 30 2012
parent reply "H. S. Teoh" <hsteoh quickfur.ath.cx> writes:
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
Nov 30 2012
parent "Chopin" <robert.bue gmail.com> writes:
Amazing! It works! Thank you so much :)
Nov 30 2012