Rust: Tracking issue for std::default::default()

Created on 5 Jun 2020  路  8Comments  路  Source: rust-lang/rust

The feature gate for the issue is #![feature(default_free_fn)].

Addition of a free default() function to the default module.

When constructing a value using Default::default() the word "default" is repeated twice.

See https://github.com/rust-lang/rust/pull/73001 for additional details, including alternative solutions.

Steps

Unresolved Questions

  • Should the language allow direct import of trait methods, this feature becomes redundant. This addition is likely to be stabilized only when we decide that direct import of trait methods is will not be supported.

Implementation history

B-unstable C-tracking-issue Libs-Small Libs-Tracked T-libs

Most helpful comment

FWIW I think it is often still good style to say T::default() to communicate to the human reader the type being constructed away, which can be declared very far away in the code (even in a different file).

All 8 comments

@rustbot modify labels to +F-default_free_fn

You call out Default::default() in the description here - but why aren't T::default() or <$any_type>::default() good alternatives? They work today, if the Default trait is in scope, and it is in the prelude.

T::default() is convenient when the compiler can not infer the type. But when it can, T because redundant. Here is a discussion where this was mentioned as well: https://github.com/rust-lang/rust/pull/73001

FWIW I think it is often still good style to say T::default() to communicate to the human reader the type being constructed away, which can be declared very far away in the code (even in a different file).

FWIW I think it is often still good style to say T::default() to communicate to the human reader the type being constructed away, which can be declared very far away in the code (even in a different file).

I do no think this change takes away that and in certain cases you have to say T::default(). The change allows you to say default() in cases where you are currently saying Default::default().

You call out Default::default() in the description here - but why aren't T::default() or <$any_type>::default() good alternatives? They work today, if the Default trait is in scope, and it is in the prelude.

Default::default() happens a lot in struct displays, with StructType { ..., StructType::default() } being as redundant as StructType { ..., Default::default() }.

A bit late to the party, but why not just a use Default::default() in the prelude instead ?
That's less code to inline for the optimizer.

A bit late to the party, but why not just a use Default::default() in the prelude instead ?
That's less code to inline for the optimizer.

I am not sure I understand exactly what are you suggesting. But if the motivation described in the issues summary is not enough, please, read the discussion in https://github.com/rust-lang/rust/pull/73001

Was this page helpful?
0 / 5 - 0 ratings

Related issues

withoutboats picture withoutboats  路  213Comments

nikomatsakis picture nikomatsakis  路  236Comments

withoutboats picture withoutboats  路  202Comments

Mark-Simulacrum picture Mark-Simulacrum  路  681Comments

Centril picture Centril  路  382Comments