www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - delegate type from function signature

reply Nub Public <nubpublic gmail.com> writes:
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
parent reply Robert Clipsham <robert octarineparrot.com> writes:
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
parent Nub Public <nubpublic gmail.com> writes:
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