www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Assign any event kind to a single templatized function ?

reply Basile B. <b2.temp gmx.com> writes:
It's almost a "yeah". However this doesn't work with ref 
parameters. Any idea how to make this work, keeping the 
simplicity of the concept ?

°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°
module runnable;

import std.stdio;

struct Foo
{
     void delegate(int) event1;
     void delegate(int,int) event2;
     void delegate(int,ref int) event3;
}

struct Handler
{
     void handle(A...)(A a){writeln(a);}
     void handleref(A...)(/*auto ref*/ A a){writeln(a);}
}

void main(string[] args)
{
     import std.traits;
     Foo foo;
     Handler handler;
     foo.event1 = &handler.handle!(Parameters!(foo.event1));
     foo.event2 = &handler.handle!(Parameters!(foo.event2));
     foo.event3 = &handler.handleref!(Parameters!(foo.event2)); // 
?
}
°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°
Sep 05 2016
next sibling parent Basile B. <b2.temp gmx.com> writes:
On Monday, 5 September 2016 at 13:44:53 UTC, Basile B. wrote:

Typo, last line should be:

     foo.event3 = &handler.handleref!(Parameters!(foo.event3));
 
But it still doesnt work.
Sep 05 2016
prev sibling parent reply ag0aep6g <anonymous example.com> writes:
On 09/05/2016 03:44 PM, Basile B. wrote:
 °°°°°°°°°°°°°°°°°°°°°°°°°°°°°°
 module runnable;

 import std.stdio;

 struct Foo
 {
     void delegate(int) event1;
     void delegate(int,int) event2;
     void delegate(int,ref int) event3;
 }

 struct Handler
 {
     void handle(A...)(A a){writeln(a);}
     void handleref(A...)(/*auto ref*/ A a){writeln(a);}
 }

 void main(string[] args)
 {
     import std.traits;
     Foo foo;
     Handler handler;
     foo.event1 = &handler.handle!(Parameters!(foo.event1));
     foo.event2 = &handler.handle!(Parameters!(foo.event2));
     foo.event3 = &handler.handleref!(Parameters!(foo.event2)); // ?
 }
 °°°°°°°°°°°°°°°°°°°°°°°°°°°°°°
You can pass the delegate type itself by alias. Then Parameters carries over the ref. Not sure if that's well-defined or if it just happens to work. void handlef(F)(Parameters!F a){writeln(a);} ... foo.event3 = &handler.handlef!(typeof(foo.event3));
Sep 05 2016
next sibling parent ag0aep6g <anonymous example.com> writes:
On 09/05/2016 04:00 PM, ag0aep6g wrote:
 You can pass the delegate type itself by alias.
[...]
     void handlef(F)(Parameters!F a){writeln(a);}
Don't know why I wrote "by alias". Clearly no alias there.
Sep 05 2016
prev sibling parent Basile B. <b2.temp gmx.com> writes:
On Monday, 5 September 2016 at 14:00:04 UTC, ag0aep6g wrote:
 On 09/05/2016 03:44 PM, Basile B. wrote:
 [...]
You can pass the delegate type itself by alias. Then Parameters carries over the ref. Not sure if that's well-defined or if it just happens to work. void handlef(F)(Parameters!F a){writeln(a);} ... foo.event3 = &handler.handlef!(typeof(foo.event3));
Nice !
Sep 05 2016