guard condition for a callable thingy, with THESE arguments

cy via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Sat May 14 18:59:15 PDT 2016


I take callbacks on occasion, and I don't really care if they're 
a delegate, or a function, or a callable object, and I can assert 
that in a template:

void foo(Callable)(Callable callback) if(isSomeFunction!Callable 
|| isCallable!Callable) {
  ...
}

That works, but it doesn't show you what arguments the callable 
will take, or how many, or what its return type should be.

You can of course read the body of the `foo` function to spy out 
where the text "callable(" might be, but it'd be really nice if I 
could put that information in my guard condition somehow. What I 
want is something like this.

bool default_callback(int foo, string bar, Something baz) {
  ...
}

void foo(Callable)(Callable callback) 
if(calledTheSameAs!(Callable,default_callback)) {
  ...
}

or

void foo(Callable)(Callable callback) 
if(calledTheSameAs!(Callable,bool function(int,string,Something)) 
{
  ...
}

Is that possible to do? Has it already been done?


More information about the Digitalmars-d-learn mailing list