I’m getting tired of Default::default() everywhere. I’d like to do just default().
Could we have std::default() or std::default::default() like this:
fn default<T:Default>()-> T {
std::default::Default::default() // gosh, that's a lot of default, isn't it?
}
?
This way I could just use std::default and “be lazy”. I think readability wouldn’t suffer (quite the opposite!) - Default is so common and idiomatic, the name of the function explains it all, Default:: prefix is just stuttering, and the definition is just one racer jump away anyway.
I do wish that too... but it could be tricky from the POV of the type system...
Namely, what do you do with the type parameters of the function and where do you put the type parameter needed for T: Default -- can you specify it even (or is it a sort of arg: impl Trait kinda thing)?
As std::default(). You’d have to use default 4+ times for the use to be worth it. Even then, <_>:: is not so hard to type, so I never bother trying to shorten it more.
My code is typically very heavy with default. Default trait is just a wonderful idea, and I implement it for every type for which it makes sense.
Typing <_>:: is a chore, come on.
Also, just look at Default::default() or <_>::default(). It's just eye soring. It must make people reading Rust code think there's something terribly wrong with the language when people just keep doing it. Either stuttering or some weird characters. Meh.
All of that is subjective, and I don't agree with any of it. I have no problem typing or reading <_>::. I use Default all the time, and don't even have to think about the <_>:: part.
There’s an open issue about this somewhere in rust-lang/rust or rust-lang/rfcs repo.
This is certainly possible and more or less equivalent to use Enum::Variant;, someone just needs to do the implementation.
It’s nice to see that the ecosystem now gravitates more towards Default instead of argumentless new() functions.
I’ve been thinking about a possible feature that may be related and may also reduce the need to explicitly type Default::default() over and over again - it’d be nice if we could use partial FRU/auto-derive for the Default impl.
From my experience, structs usually have a few key data members that have to be explicitly initialized but also have a lot of supplementing data, all of which can be Default::default()ed.
MyData {
aux: important_bit.something(),
key: important_bit,
_: Default::default(), // open for bike-shedding
// ...Default::default() FRU not possible since there's no `impl Default for MyData`
Would that be considered useful and not expend the language complexity/quirkness budget?
I prefer to encourage the style @anon19897381 shows, in which the type is included in the expression, to help keep things clearer. Unless you’re doing Foo { bar, baz, ..Default::default() }, in which cause its clear enough.
I like that. Though in many cases I would rather have type inference figure out the type since the variable name makes it already obvious to the reader.
Anyway, since I made a crate, now in Rust 2018 edition I can literally do use default::default;, which is even better than use std::default::default();.