Skip to content
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

Remove remark about poor code style #37503

Merged
merged 2 commits into from
Nov 12, 2016
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 1 addition & 21 deletions src/doc/book/traits.md
Original file line number Diff line number Diff line change
Expand Up @@ -243,27 +243,7 @@ to know more about [operator traits][operators-and-overloading].
# Rules for implementing traits

So far, we’ve only added trait implementations to structs, but you can
implement a trait for any type. So technically, we _could_ implement `HasArea`
for `i32`:

```rust
trait HasArea {
fn area(&self) -> f64;
}

impl HasArea for i32 {
fn area(&self) -> f64 {
println!("this is silly");

*self as f64
}
}

5.area();
```

It is considered poor style to implement methods on such primitive types, even
though it is possible.
implement a trait for any type such as `i32`.
Copy link
Member

Choose a reason for hiding this comment

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

leave the example in I guess?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Maybe. Then I would suggest to change the implementation to

impl HasArea for i32 {
    fn area(&self) -> f64 {
        println!("this is silly");

        0.0
    }
}

to depict the silliness of calculating the area of a scalar.

Copy link
Member

Choose a reason for hiding this comment

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

How about .squared()?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

That would actually be bad style (imho). One cannot implement a meaningful area for i32, only for the newtype struct Square(i32). This is why I wanted to remove the example altogether because that possibly lengthy discussion is out of scope of that particular paragraph.

Copy link
Member

Choose a reason for hiding this comment

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

@nwin Can you suggest an alternative non-silly example that we could put here?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

We could make up a trait like Complex and output the real and imaginary part in two methods.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I've just come up with a better idea.

trait ApproxEqual {
    fn approx_equal(&self, other: &Self);
}
impl ApproxEqual for f64 {
    fn approx_equal(&self, other: &Self) {
        // Impementation to be improved for the actual example
        // Only valid for numbers close to 1.0
        (self - other).abs() < ::std::f64::EPSILON 
}


This may seem like the Wild West, but there are two restrictions around
implementing traits that prevent this from getting out of hand. The first is
Expand Down