-
Notifications
You must be signed in to change notification settings - Fork 13.3k
Add min and max methods to Ord and PartialOrd #16067
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
The nature of This seems like a strong negative. |
I was aware of that issue, which is why I added the doc comments, but perhaps it should return an My reasoning was that when you compare identical numbers of some kind, you still want a return value, and returning the first value (self) seemed like a sane default. If it was to return an option instead, what would be the desired behaviour when the values are equal? |
|
||
/// This method returns the greater of two values. | ||
/// If the values are equal or cannot be compared, the value which this | ||
/// method was called on (self) is returned. |
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.
Docstring formatting is wrong.
/// Returns the greater of two values.
///
/// If the values are equal or cannot be compared, the receiver is returned.
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.
That is, rustdoc parses the first paragraph of the doc string as the main functionality-summary, so the extra info should be it's own paragraph.
I'm with @huonw, partial ordering seems like a reason not to have this. If we did have this, I think it would have to return Perhaps the right approach is to add functions #[inline]
pub fn partial_min<T: PartialOrd>(v1: T, v2: T) -> Option<T> {
match v1.partial_cmp(&v2) {
None => None,
Some(Less) => Some(v1),
_ => Some(v2),
}
}
#[inline]
pub fn partial_max<T: PartialOrd>(v1: T, v2: T) -> Option<T> {
match v1.partial_cmp(&v2) {
None => None,
Some(Greater) => Some(v1),
_ => Some(v2)
}
} |
Okay, I added the methods to Ord instead, and added partial methods which return an option to PartialOrd. One thing I'm not sure about is the following:
If I called |
But that's not how the already-marked- Also, I am not convinced it's appropriate to be adding |
+1 to what @kballard said. min/max should be free standing functions, not methods. That way they can be pulled in unambiguously when desired. Losing clean chaining is minor in comparison, imo. |
Hmm. My original idea for this was because integer types were lacking min/max methods (whereas floats do), but then I thought that there would probably be a use case for most other Ord/PartialOrd types. Would it be worth having min/max methods for just integers instead? (Edit: It appears some trailing whitespace slipped in at the last minute.) |
…l-inside-types, r=Veykril fix: no code action 'introduce_named_generic' for impl inside types Fix rust-lang#15734. ### Changes Made - Find params in `ancestors` instead of just `parent` - Added tests (`replace_impl_with_mut` and `replace_impl_inside`)
I would've also changed the
min
andmax
functions incore::cmp
, but they were marked as stable and operated by-value rather than by-reference, so I want to check if it was okay.The new methods return the greater/lesser of two values, and if an ordering cannot be found or the two values are equal, return the value that the method was called on (
self
).As for the
min
/max
functions, would it be a good idea to a) change them to use the min/max methods provided fromPartialOrd
, and b) only requirePartialOrd
rather thanOrd
. Or should they just be left alone as they are "stable".