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

Support closures as delegates #332

Open
kennykerr opened this issue Sep 30, 2020 · 3 comments
Open

Support closures as delegates #332

kennykerr opened this issue Sep 30, 2020 · 3 comments
Labels
enhancement New feature or request

Comments

@kennykerr
Copy link
Collaborator

kennykerr commented Sep 30, 2020

Rust/WinRT doesn't yet automatically bind a lambda/closure to a delegate parameter. Today you need to create the delegate yourself. It looks something like this:

list.current_item_changed(TypedEventHandler::new(|_sender, _args| {
    // do stuff here
    Ok(())
}))?;

In the future you should be able to write simply:

list.current_item_changed(|_sender, _args| {
    // do stuff here
    Ok(())
})?;

The difficulty is in somehow specializing the winrt::Param generic for delegates to automatically create a WinRT delegate as needed while still supporting delegate objects for pass-through.

Alternatively, we could just use FnMut as the generic param for delegates. That would probably provide the best experience in most cases, but would suffer from "over-wrapping" delegate objects that are merely being passed through.

@kennykerr kennykerr added the enhancement New feature or request label Sep 30, 2020
@rylev
Copy link
Contributor

rylev commented Oct 2, 2020

This might not be possible to do since conceptually we need to ensure methods which take delegates are generic over some type which is either a delegate which takes some type that implements FnMut or another type which also implements the same FnMut trait. I believe this would require higher-kinded types since the generic delegate type is itself generic over some type.

@kennykerr
Copy link
Collaborator Author

Challenge accepted. 🤜

@kennykerr
Copy link
Collaborator Author

Given that passing a closure is by far the most common scenario, we could make that the default and provide a XxxWithDelegate "overload" for when you absolutely must pass an existing delegate object.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants