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

Add min/max/clamp to Ord #12068

Closed
pongad opened this issue Feb 6, 2014 · 11 comments
Closed

Add min/max/clamp to Ord #12068

pongad opened this issue Feb 6, 2014 · 11 comments

Comments

@pongad
Copy link
Contributor

pongad commented Feb 6, 2014

#12061 removed Orderable, so now we need to add min/max/clamp methods back to Ord. Adding them right now creates a lot of ambiguous method error. This is probably best left until UFCS lands.

@thestinger
Copy link
Contributor

There are already min and max functions in std::cmp. I don't think these should be methods because reserving the min and max method names for every type implementing Ord to too broad.

@emberian
Copy link
Member

emberian commented Feb 6, 2014

The benefit being that floats could use fmin/fmax.

@emberian
Copy link
Member

emberian commented Feb 6, 2014

...but we could probably just expose those directly

@thestinger
Copy link
Contributor

I don't think floating point numbers should implement Ord. They can't satisfy any sane definition of Ord with the hardware comparison operations.

@pongad
Copy link
Contributor Author

pongad commented Feb 6, 2014

@thestinger Could you elaborate? I thought floating point comparison is quite well defined.

@thestinger
Copy link
Contributor

The hardware comparison operations do not provide a total ordering. IEEE754 does provide a separate total ordering but hardware doesn't implement it and Rust doesn't use it for the comparison operators.

If you can't say that x == x is always true or that !(a < b) && !(b < a) implies a == b then what do the Eq and Ord traits actually mean? They're not usable by sort, HashMap or TreeMap...

@pongad
Copy link
Contributor Author

pongad commented Feb 7, 2014

I see your point now. There are obviously problems when you do floating points, though I'm not sure if it's wise to not implement the traits entirely. Users with experience with floating points ought to know of these drawbacks. My current (not too informed) opinion is that implementing Eq and Ord anyway is more pragmatic.

@thestinger
Copy link
Contributor

Rust has TotalEq and TotalOrd to work around this issue... so implementing it anyway just means no one can use Eq and Ord (TreeMap and sort need TotalOrd). I think the only sane choices are using the IEEE754 total ordering for floats or not implementing the comparison traits and operators for them. SML does the right thing here.

@brendanzab
Copy link
Member

I think the only sane choices are using the IEEE754 total ordering for floats or not implementing the comparison traits and operators for them. SML does the right thing here.

Agreed. Should we have a separate fmin and fmax for floats?

@thestinger
Copy link
Contributor

Yes, I think the floating point modules should define min and max functions or they can be defined on a Float trait.

@thestinger
Copy link
Contributor

I think we have enough issues open about this already. There's a strong consensus that the current situation is bad but no consensus on how to deal with floating point. However, it's clear that it's unable to provide the requirements expected of Eq and Ord while still using the usual comparison semantics so there's not a strong reason to change min, max and clamp to methods. The min and max method names should really not be reserved by such a common trait.

flip1995 pushed a commit to flip1995/rust that referenced this issue Jan 11, 2024
Add .as_ref() to suggestion to remove .to_string()

The case of `.to_owned().split(…)` is treated specially in the `unnecessary_to_owned` lint. Test cases check that it works both for slices and for strings, but they missed a corner case: `x.to_string().split(…)` when `x` implements `AsRef<str>` but not `Deref<Target = str>`. In this case, it is wrong to suggest to remove `.to_string()` without adding `.as_ref()` instead.

Fix rust-lang#12068

changelog: [`unnecessary_to_owned`]: suggest replacing `.to_string()` by `.as_ref()`
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

No branches or pull requests

4 participants