Re: Generalizing over function pointers and delegates

2019-02-15 Thread Bastiaan Veelo via Digitalmars-d-learn
On Friday, 15 February 2019 at 17:28:45 UTC, H. S. Teoh wrote: On Fri, Feb 15, 2019 at 05:40:39PM +0100, ag0aep6g via Digitalmars-d-learn wrote: Your fun_to_dlg fails when the function has parameters. Yes. Delegates are basically syntactic sugar for a function pointer with an implicit first

Re: Generalizing over function pointers and delegates

2019-02-15 Thread H. S. Teoh via Digitalmars-d-learn
On Fri, Feb 15, 2019 at 05:40:39PM +0100, ag0aep6g via Digitalmars-d-learn wrote: > On 15.02.19 15:20, Bastiaan Veelo wrote: > > Exploiting this, it is possible to explicitly convert a function > > pointer into a delegate [2]: > > ``` > > Ret delegate(Args args) fun_to_dlg(Ret, Args...)(Ret

Re: Generalizing over function pointers and delegates

2019-02-15 Thread Bastiaan Veelo via Digitalmars-d-learn
On Friday, 15 February 2019 at 16:40:39 UTC, ag0aep6g wrote: Your fun_to_dlg fails when the function has parameters. Hah ok. std.functional.toDelegate() does work in its place though. As far as I see, it would be possible make the conversion would work by changing how a delegate's context is

Re: Generalizing over function pointers and delegates

2019-02-15 Thread ag0aep6g via Digitalmars-d-learn
On 15.02.19 15:20, Bastiaan Veelo wrote: Exploiting this, it is possible to explicitly convert a function pointer into a delegate [2]: ``` Ret delegate(Args args) fun_to_dlg(Ret, Args...)(Ret function(Args args) fun) {     Ret delegate(Args) dlg;     dlg.funcptr = fun;     return dlg; }

Re: Generalizing over function pointers and delegates

2019-02-15 Thread Bastiaan Veelo via Digitalmars-d-learn
On Friday, 15 February 2019 at 14:30:45 UTC, Alex wrote: There is https://dlang.org/library/std/functional/to_delegate.html Ah, there it is :-) Thanks. A templated function also works. ``` int genfun(F)(F dg) {return dg();} ​ int top_level() {return -1;} ​ void main() { int nested()

Re: Generalizing over function pointers and delegates

2019-02-15 Thread Alex via Digitalmars-d-learn
On Friday, 15 February 2019 at 14:20:44 UTC, Bastiaan Veelo wrote: Given a function taking a delegate, for example ``` int fun(int delegate() dg) {return dg();} ``` Sometimes we need to call `fun` with a pointer to a nested function and other times with a pointer to a top level function. As

Generalizing over function pointers and delegates

2019-02-15 Thread Bastiaan Veelo via Digitalmars-d-learn
Given a function taking a delegate, for example ``` int fun(int delegate() dg) {return dg();} ``` Sometimes we need to call `fun` with a pointer to a nested function and other times with a pointer to a top level function. As function pointers do not implicitly convert to delegates, this does