On Monday, 20 March 2017 at 17:58:32 UTC, Moritz Maxeiner wrote:
2) Create a template that can be used like this (you'll want to
consult std.traits for this):
void foo (delegateOf!(T.method) fptr) {}
This may sound harder that it is, btw. A template that does
exactly that is the following (template constraints that verify
`method` to actually be a member method left out):
template DelegateTypeOf(alias method)
{
import std.array : replaceFirst;
import std.string : format;
enum fnType = typeof(&method).stringof;
enum dgType = fnType.replaceFirst("function", "delegate");
mixin(`alias DelegateTypeOf = %s;`.format(dgType));
}
Including the above, a full example would be the following:
interface T
{
void method(int x);
}
void foo(DelegateTypeOf!(T.method) dg)
{
dg(42);
}
class X : T
{
void method(int x)
{
import std.stdio : writefln;
writefln("Hello, world #%d!", x);
}
}
void main()
{
T t = new X;
foo(&t.method);
}