arsd icon indicating copy to clipboard operation
arsd copied to clipboard

minigui docs: event handler must be a delegate

Open whitebyte opened this issue 11 months ago • 4 comments

https://dpldocs.info/experimental-docs/arsd.minigui.Event.html

Trying to actually compile an example from this page

widget.addEventListener((KeyDownEvent ev) { ... }, /* optional arg */ useCapture );

throws arsd/minigui.d(1416): Error: static assert: "Your handler wasn't usable because it wasn't passed a delegate. Use the delegate keyword at the call site."

Marking lambda as a delegate solves the issue:

widget.addEventListener(delegate(KeyDownEvent ev) { ... }, /* optional arg */ useCapture );

no biggie but it's worth mentioning in the docs. Also, useCapture isn't documented anywhere

whitebyte avatar Jan 02 '25 19:01 whitebyte

yeah if you use local variables it is automatically a delegate so in most cases it will work but an empty function (or just a writeln logger) won't.

useCapture means the event is triggered on the capture phase instead of the bubble phase. the docs talk about this but it is mostly a javascript thnig you can look up too

maybe ill rewrite that soon

adamdruppe avatar Jan 02 '25 20:01 adamdruppe

it will work but an empty function (or just a writeln logger) won't.

That was exactly my case. I just slapped a writeln to check if it works. Then why minigui insists on a delegate? If a programmer is ok with a plain function that doesn't capture context, minigui should also be ok

whitebyte avatar Jan 03 '25 09:01 whitebyte

It just requires adding like 12 function overloads for the function to delegate adapters.... easier to just put the word at the call site lol

adamdruppe avatar Jan 03 '25 12:01 adamdruppe

OpenD could probably implicitly convert function to delegate, codegen wise you can do it and there's no loss of information (you can do it in library too, put a thunk in the funcptr then ptr is the actual funcptr). So solid maybe putting that right in the compiler but might also cause side effects.

adamdruppe avatar Jan 03 '25 14:01 adamdruppe