pwiecz / go-fltk

A simple wrapper around FLTK 1.4 library
MIT License
121 stars 22 forks source link

callbacks can't return error #87

Closed vlappa closed 1 year ago

vlappa commented 1 year ago

Handling errors is important in Go, but callbacks are expected to be of type func() in go-fltk. Shouldn't that become: func() error?

https://github.com/pwiecz/go-fltk/blob/main/menu.go#L48

pwiecz commented 1 year ago

Can you, please, elaborate, how would you use this error value in menu callback?

mark-summerfield commented 1 year ago

You don't need to return an error: and in fact in a callback you can't since the callback is called by the system not by you.

The solution for handling errors in callbacks is to use a closure, e.g.,

widget.SetCallback(func() { 
if err := doSomething(); err != nil {
    app.handleError(err) // app is some type that's been captured by the closure
}
})
vlappa commented 1 year ago

I see, thx. Callbacks are a bit magical to me still. I'm starting to see how closures can be useful.