Why function does not work with delegate

Alex AJ at gmail.com
Wed May 22 22:33:52 UTC 2019


In gtkD one can use a lambda directly:

     X.addOnButtonPress((GdkEventButton* e, Widget w) ...

but if I try move the lambda in to a variable so I can use it 
with multiple handlers, I get an error:

     // x is a function and does not work
     auto x = (GdkEventButton* e, Widget w) ...
     X.addOnButtonPress(x);

etc...

It's because the second case treats x as a function while the 
first it is a delegate and addOnButtonPress requires a delegate...

Why is x not a delegate?

Now, if I reference a variable outside the scope of the lambda it 
turns magically in to a delegate and works!

But that is an ugly hack!

// now x is a delegate and works
auto x = (GdkEventButton* e, Widget w) { auto q = X; ...


Now sure I can hack x and use some template to turn it in to a 
fake delegate but then that is dangerous.

I suppose one could have a template that only converts it if it 
is a function and then that will deal with both cases and might 
work...

But why?
Is there any way to force it to not reduce the delegate to a 
function which is obviously an optimization when nothing accesses 
the outside context.






			


More information about the Digitalmars-d-learn mailing list