Thread overview
delegate type from function signature
Jun 26, 2011
Nub Public
Jun 26, 2011
Robert Clipsham
Jun 26, 2011
Nub Public
June 26, 2011
Is it possible to get the signature of a function and make a delegate type from it?


Something like this:

bool fun(int i) {}

alias (signature of fun) DelegateType;

DelegateType _delegate = delegate(int i) { return i == 0; };
June 26, 2011
On 26/06/2011 08:08, Nub Public wrote:
> Is it possible to get the signature of a function and make a delegate
> type from it?
>
>
> Something like this:
>
> bool fun(int i) {}
>
> alias (signature of fun) DelegateType;
>
> DelegateType _delegate = delegate(int i) { return i == 0; };

----
alias typeof(&fun) FuncType;
FuncType _function = function(int i) { return i == 0; };
----

Note that fun() here has no context, so FuncType is an alias for bool function(int). If you truely want a delegate type, you could use the following:

----
import std.traits;
template DelegateType(alias t)
{
    alias ReturnType!t delegate(ParameterTypeTuple!t) DelegateType;
}
DelegateType!fun _delegate = (int i) { return i == 0; };
----

The other alternative is to just use auto, there's no guarantee it will be the same type as the function though:
----
auto _delegate = (int i) { return i == 0; }
----

Hope this helps.

-- 
Robert
http://octarineparrot.com/
June 26, 2011
On 6/26/2011 5:45 PM, Robert Clipsham wrote:
> template DelegateType(alias t)
> {
>      alias ReturnType!t delegate(ParameterTypeTuple!t) DelegateType;
> }


Thanks. This is exactly what I need.