Accepting function or delegate as function argument

chmike via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Wed May 4 07:54:39 PDT 2016


I have implemented the following class (simplified ;) )

----
class Foo(K,T) {
     this(T delegate (K) factory) { m_factory = factory; }
     T delegate (K) m_factory;
     T bar(K key) { return m_factory(key); }
}

string dummyFactory(string key) { return "Hello "~key; }

void main()
{
     auto foo = new Foo!(string,string)(&dummyFactory);
     writeln(foo.bar("world"));
}
----

The compiler complains that dummyFactory is a pointer to a 
function and not a delegate.
How can I modify the class definition so that it accepts a 
function or a delegate transparently from the user perspective ?

Two constructors, one accepting a function and the other one 
accepting a delegate would do the job for the API. Is there a 
simple method to convert a function pointer into a delegate 
pointer that is also efficient ?


More information about the Digitalmars-d-learn mailing list