Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Boxed Fns / F: Fn type parameters? #307

Open
dhardy opened this issue Apr 25, 2022 · 1 comment
Open

Boxed Fns / F: Fn type parameters? #307

dhardy opened this issue Apr 25, 2022 · 1 comment
Labels
discussion Requires analysis/discussion

Comments

@dhardy
Copy link
Collaborator

dhardy commented Apr 25, 2022

Widgets like CheckBox, TextButton take an optional closure, called on activation/toggle. Options to store this:

<F: FnMut(&mut EventMgr, bool)>
...
on_toggle: Option<F>,

Using a type parameter is the "rustic" option. Problem: the parametrized widget's type is not nameable. With RFC#2524 or RFC#2515 this may be less of an issue, at least for types only used locally. A compound widget such as a pre-built dialog might contain several type parameters, and having to include these as part of the type could be a pain. (This is event a problem if the closure's type is fixed in the compound widget, since we can't name the type of functions.)

For the same reason, we don't want a lifetime parameter, which implies that all captured variables must be moved.

on_toggle: Option<Box<dyn FnMut(&mut EventMgr, bool)>>,

Problem: doesn't support Clone, which is sometimes useful.

on_toggle: Option<Rc<dyn Fn(&mut EventMgr, bool)>>,

Use Rc and Fn (not FnMut). Okay, but now we don't support mutable captured variables. (Alternative: use Rc<RefCell<dyn FnMut ..>>. Viable, but not sufficiently useful for the extra functionality?)

on_toggle: Option<fn(&mut EventMgr, bool)>,

Use of only a function pointer is simpler, automatically supports Clone, and since we already had Fn(..) + 'static, the only functionality we lose is capturing of moved, constant variables. So is this the better option?

@dhardy dhardy added the discussion Requires analysis/discussion label Apr 25, 2022
@dhardy dhardy changed the title Boxed Fns? Boxed Fns / F: Fn type parameters? Apr 25, 2022
@dhardy
Copy link
Collaborator Author

dhardy commented Jul 16, 2022

Note: having widgets support Clone was considered important, but barely seems to have any use in practice (in the few cases where multiple instances of the same widget are desired, a constructor method works fine).

On the other hand, these action handlers are normally only used to send a message to the real handler from the context of some parent widget, which it turns out is a neat event-handling pattern — in other words, I see no good rationale for why we'd want to support FnMut.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
discussion Requires analysis/discussion
Projects
None yet
Development

No branches or pull requests

1 participant