digitalmars.D.learn - delegate type from function signature
- Nub Public (6/6) Jun 26 2011 Is it possible to get the signature of a function and make a delegate
- Robert Clipsham (25/31) Jun 26 2011 ----
- Nub Public (2/6) Jun 26 2011 Thanks. This is exactly what I need.
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; };
Jun 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/
Jun 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.
Jun 26 2011