www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Convert delegate or function type to other.

reply Rufus Smith <RufusSmith indi.com> writes:
Suppose I have the following: alias func = void function(int);

Is there a way to convert it automatically to something the same 
type except of delegate: alias del = toDel(func) = void 
delegate(int);?
Jul 18 2016
next sibling parent reply Jacob Carlborg <doob me.com> writes:
On 2016-07-18 20:49, Rufus Smith wrote:
 Suppose I have the following: alias func = void function(int);

 Is there a way to convert it automatically to something the same type
 except of delegate: alias del = toDel(func) = void delegate(int);?
https://dlang.org/phobos/std_functional.html#toDelegate -- /Jacob Carlborg
Jul 18 2016
parent reply Rufus Smith <RufusSmith indi.com> writes:
On Monday, 18 July 2016 at 18:51:29 UTC, Jacob Carlborg wrote:
 On 2016-07-18 20:49, Rufus Smith wrote:
 Suppose I have the following: alias func = void function(int);

 Is there a way to convert it automatically to something the 
 same type
 except of delegate: alias del = toDel(func) = void 
 delegate(int);?
https://dlang.org/phobos/std_functional.html#toDelegate
No, that converts an actual function. I need to create a new alias from the old one. I'd also like to be able to create a delegate with a different context pointer.
Jul 18 2016
parent Jacob Carlborg <doob me.com> writes:
On 2016-07-18 21:19, Rufus Smith wrote:

 No, that converts an actual function. I need to create a new alias from
 the old one.
You mean the actual type? John's answer will give you that.
 I'd also like to be able to create a delegate with a different context
 pointer.
You can explicitly set the context pointer using the "ptr" property: class Foo { void bar() {} } void delegate() dg; dg.ptr = cast(void*) new Foo; dg.funcptr = &Foo.bar; // not sure if this requires a cast -- /Jacob Carlborg
Jul 18 2016
prev sibling parent reply John <johnch_atms hotmail.com> writes:
On Monday, 18 July 2016 at 18:49:22 UTC, Rufus Smith wrote:
 Suppose I have the following: alias func = void function(int);

 Is there a way to convert it automatically to something the 
 same type except of delegate: alias del = toDel(func) = void 
 delegate(int);?
import std.traits; alias del = ReturnType!func delegate(Parameters!func);
Jul 18 2016
parent Rufus Smith <RufusSmith indi.com> writes:
On Monday, 18 July 2016 at 20:15:30 UTC, John wrote:
 On Monday, 18 July 2016 at 18:49:22 UTC, Rufus Smith wrote:
 Suppose I have the following: alias func = void function(int);

 Is there a way to convert it automatically to something the 
 same type except of delegate: alias del = toDel(func) = void 
 delegate(int);?
import std.traits; alias del = ReturnType!func delegate(Parameters!func);
Thanks, I guess that allows me to change the parameters too!
Jul 18 2016