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

Implement Default for fn types and closures. #3000

Open
Diggsey opened this issue Oct 7, 2020 · 3 comments
Open

Implement Default for fn types and closures. #3000

Diggsey opened this issue Oct 7, 2020 · 3 comments

Comments

@Diggsey
Copy link
Contributor

Diggsey commented Oct 7, 2020

I could avoid some unsafe code if the types of functions and closures-without-captures implemented the Default trait.

Theoretically you could extend this to closures that only capture things implementing Default, but starting with only closures that have no captures seems like a safer bet, and it can always be extended later.

Example:

fn call_by_type<F: Fn() + Copy + Default>() {
    let f = F::default();
    f();
}

fn wrapper<F: Fn() + Copy + Default>(_f: F) {
    call_by_type::<F>();
}

fn foo() {
    println!("Hello, world!");
}

fn main() {
    wrapper(foo);
}
@H2CO3

This comment has been minimized.

@Diggsey

This comment has been minimized.

@H2CO3

This comment has been minimized.

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

No branches or pull requests

2 participants