Skip to content

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

Merged
merged 7 commits into from
Dec 6, 2017
Merged

new idiom: Default trait #48

merged 7 commits into from
Dec 6, 2017

Conversation

llogiq
Copy link
Contributor

@llogiq llogiq commented Dec 31, 2016

Please review. Thank you!

}

fn interestingness(i: &Interesting) -> usize {
if really {
Copy link

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 ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed. Thank you.

Copy link
Collaborator

@lambda-fairy lambda-fairy left a 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.


## Description

Many types in Rust have a [Constructor]. However, this is *specific* to the
Copy link
Collaborator

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.

```rust
// note that we can simply auto-derive Default here.
#[derive(Default)]
struct Interesting {
Copy link
Collaborator

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.

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.
Copy link
Collaborator

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'.

}
}
```

Copy link
Collaborator

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.

not be "default"
- The [`Default`] documentation (scroll down for the list of implementors)
- [`Option::unwrap_or_default()`]

Copy link
Collaborator

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/

@killercup
Copy link

Some talk around new and Default with @llogiq and yours truely: 1 and 2 😄

@sanmai-NL
Copy link

sanmai-NL commented Nov 17, 2017

Should this PR be merged still? Can we take the last paragraph of C-COMMON-TRAITS into consideration (e.g., by referring to that, not repeating stuff, perhaps expanding that and slimming down this)?

@llogiq
Copy link
Contributor Author

llogiq commented Dec 5, 2017

@nrc @lfairy I think I adressed all your requests, so what's keeping this from being merged?

@nrc nrc merged commit f6acf26 into rust-unofficial:master Dec 6, 2017
@nrc
Copy link
Collaborator

nrc commented Dec 6, 2017

I don't think I ever saw the new commits, sorry.

@llogiq llogiq deleted the default branch December 6, 2017 22:05
@llogiq
Copy link
Contributor Author

llogiq commented Dec 6, 2017

No need to be sorry, I've been swamped with work recently, too.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants