-
Notifications
You must be signed in to change notification settings - Fork 385
new idiom: Default trait #48
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
Conversation
idioms/default.md
Outdated
} | ||
|
||
fn interestingness(i: &Interesting) -> usize { | ||
if really { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should this really
be i.really
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed. Thank you.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks! My issues are pretty minor, and shouldn't take long to fix.
idioms/default.md
Outdated
|
||
## Description | ||
|
||
Many types in Rust have a [Constructor]. However, this is *specific* to the |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit: "constructor" should be lowercase here, at least.
I'm not sure if the Constructor pattern should be capitalized or not, but AFAIK when we talk about a constructor function it is always in lowercase.
idioms/default.md
Outdated
```rust | ||
// note that we can simply auto-derive Default here. | ||
#[derive(Default)] | ||
struct Interesting { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Default
is often used for configuration structs -- I'd prefer if we use a more configuration-like theme for the example.
idioms/default.md
Outdated
type; Rust cannot abstract over "everything that has a `new()` method". To | ||
allow this, the [`Default`] trait was conceived, which can be used with | ||
containers and other generic types (e.g. see [`Option::unwrap_or_default()`]). | ||
Notably, some containers already implement it where applicable. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Worth noting, though probably further down, is that new
and default
do different things - new
typically takes values for all or most fields (i.e., is shorthand for the literal, modulo private fields), whereas default
takes no arguments and thus creates an object 'from nothing'.
idioms/default.md
Outdated
} | ||
} | ||
``` | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Another detail worth elaborating on is when Default
can be derived, vs when it must be manually implemented.
idioms/default.md
Outdated
not be "default" | ||
- The [`Default`] documentation (scroll down for the list of implementors) | ||
- [`Option::unwrap_or_default()`] | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
shameless plug for derive(new)
- https://crates.io/crates/derive-new/
Should this PR be merged still? Can we take the last paragraph of |
I don't think I ever saw the new commits, sorry. |
No need to be sorry, I've been swamped with work recently, too. |
Please review. Thank you!