delegate type from function signature

Robert Clipsham robert at octarineparrot.com
Sun Jun 26 02:45:22 PDT 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/


More information about the Digitalmars-d-learn mailing list