On Sunday, 15 May 2016 at 01:59:15 UTC, cy wrote:
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?
use "Parameters" in the constraint or make a template that you
can reeuse.
----
import std.traits;
alias Model = void function(int, string);
void foo(Callable)(Callable callback)
if(is(Parameters!Callable == Parameters!Model)) {}
void main(string[] args)
{
static void right(int,string){}
static assert(__traits(compiles, foo(&right)));
static void wrong(int){}
static assert(!__traits(compiles, foo(&wrong)));
}
----