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);
}
Ouch. Is every std
trait going to end up as a lang item eventually? That's entirely the wrong direction 馃憥 馃槥
Ouch. Is every std trait going to end up as a lang item eventually? That's entirely the wrong direction 馃憥 馃槥
馃し that's just an implementation detail of the language. Yeah it would be nice if these implementations could live in std rather than being magic. I don't really see that as an argument against having closure and function types implement the Default
trait.
If we're going to get rid of lang items, it's not going to happen one at a time: any solution that can get rid of Clone
being a lang item can also get rid of Default
being a lang item, so we're not getting further from that goal by adding these.
I don't really see that as an argument against having closure and function types implement the Default trait.
I'm not arguing "against having closure and function types implement the Default trait." I'm arguing against Default being made into a lang item.