Skip to content

Implement Default for fn types and closures. #3000

Open

Description

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);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions