Thread overview
Accepting function or delegate as function argument
May 04, 2016
chmike
May 04, 2016
Adam D. Ruppe
May 04, 2016
Rene Zwanenburg
May 06, 2016
Gary Willoughby
May 04, 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 ?
May 04, 2016
On Wednesday, 4 May 2016 at 14:54:39 UTC, chmike wrote:
> 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 ?

Do the overload and convert function to delegate with std.functional.toDelegate

http://dpldocs.info/experimental-docs/std.functional.toDelegate.html


stored_dg = toDelegate(passed_function);
May 04, 2016
On Wednesday, 4 May 2016 at 14:54:39 UTC, chmike wrote:
> 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 ?

http://dlang.org/phobos/std_functional.html#.toDelegate

You can also make your constructor a template, constrain it on isCallable if you wish, and then use toDelegate. If the argument is already a delegate toDelegate will avoid doing extra work.
May 06, 2016
On Wednesday, 4 May 2016 at 15:23:20 UTC, Rene Zwanenburg wrote:
> On Wednesday, 4 May 2016 at 14:54:39 UTC, chmike wrote:
>> 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 ?
>
> http://dlang.org/phobos/std_functional.html#.toDelegate
>
> You can also make your constructor a template, constrain it on isCallable if you wish, and then use toDelegate. If the argument is already a delegate toDelegate will avoid doing extra work.

import std.functional;
import std.stdio;
import std.traits;

class Foo(K,T)
{
	private T delegate(K) m_factory;

	public this(T)(T factory) if (isCallable!(T))
	{
		this.m_factory = toDelegate(factory);
	}

	public T bar(K key)
	{
		return this.m_factory(key);
	}
}

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

void main()
{
    auto foo = new Foo!(string, string)(&dummyFactory);

    writeln(foo.bar("world"));
}