Join GitHub today
GitHub is home to over 31 million developers working together to host and review code, manage projects, and build software together.
Sign upTracking issue for `Zero`/`One`/`iter_arith` stabilization #27739
Comments
aturon
added
T-libs
B-unstable
labels
Aug 12, 2015
Ms2ger
referenced this issue
Aug 16, 2015
Open
Tracking: Unstable Rust feature gates used by Servo #5286
This comment has been minimized.
This comment has been minimized.
fizyk20
commented
Oct 22, 2015
|
Those traits would be really useful for generic arithmetics, I look forward to them being stabilized. Also, I have some ideas for traits that would pair up with those nicely, e.g.: |
This comment has been minimized.
This comment has been minimized.
photino
commented
Nov 1, 2015
|
I would like to see these are stabilized. They are really useful and simple enough to have a position in |
This comment has been minimized.
This comment has been minimized.
Lisoph
commented
Nov 4, 2015
aturon
added
the
I-nominated
label
Nov 4, 2015
This comment has been minimized.
This comment has been minimized.
|
Nominating for 1.6 discussion. |
This comment has been minimized.
This comment has been minimized.
|
I have multiple times tried to use Please stabilize |
This comment has been minimized.
This comment has been minimized.
|
There is an open issue with iterator sum and product regarding type inference / default type parameter fallback (#25094) I'm not sure how this impacts stabilization. |
This comment has been minimized.
This comment has been minimized.
|
Unfortunately the libs team didn't have time to decided on this in the triage meeting, so this won't enter FCP this cycle. |
alexcrichton
removed
the
I-nominated
label
Nov 5, 2015
This comment has been minimized.
This comment has been minimized.
|
I'd like to nominate at least |
sfackler
added
the
I-nominated
label
Dec 14, 2015
This comment has been minimized.
This comment has been minimized.
|
As a general point, these traits don't necessarily cover everything one might want for non-primitives. In particular, Additionally, for other types, it may not make sense to just ask for |
This comment has been minimized.
This comment has been minimized.
|
I agree that precision is important for big-floats, but I don't think that the notion of precision belongs in Zero. Something like |
This comment has been minimized.
This comment has been minimized.
|
If I’m grepping correctly,
Since we’ve already decided to reduce the scope of (Edited to remove truncated paragraph, it was the same as the previous one.) |
This comment has been minimized.
This comment has been minimized.
|
At the libs triage meeting we discussed that we may want to somewhat aggresively pursue stabilization of the methods here, and if it comes to it we can punt on stabilizing the traits involves (as we've done with other APIs in the past). @aturon however I believe would like to discuss stabilizing the tl;dr; This FCP is primarily for stabilizing the |
alexcrichton
added
final-comment-period
and removed
I-nominated
labels
Dec 17, 2015
This comment has been minimized.
This comment has been minimized.
|
The type parameter default should be removed before stabilization, it has no effect, and the lang team seems to unfortunately doubt the future of the feature altogether. |
This comment has been minimized.
This comment has been minimized.
|
@SimonSapin Your comment was cut short, but I think I agree. Zero and One are out of place as the only numeric traits in libstd. That said, it's good for everyone if One and Zero are either stabilized or removed. Unstable limbo just leads to the tricky conflicts of std's Zero vs num's Zero. |
This comment has been minimized.
This comment has been minimized.
|
|
PeterHatch
referenced this issue
Jan 3, 2016
Closed
Summing Vec<Complex<T>>: difference between core::num::Zero and num::traits::Zero #126
This comment has been minimized.
This comment has been minimized.
|
@bluss Good catch. For the record, the difference is that Given that we want a direct connection between e.g. That issue aside, the only real question with I agree with @SimonSapin's assessment. I also think that these traits are relatively harmless -- they are effectively specialized versions of So overall, I think I'm |
This comment has been minimized.
This comment has been minimized.
|
If we have concerns about future compatibility with more robust numeric trait hierarchies, we can always make these traits specifically for |
This comment has been minimized.
This comment has been minimized.
|
How about, in trait Sum: Add<Rhs=Self> {
/// Sum of an empty sequence, i.e. "zero".
fn neutral_element() -> Self;
}
trait Product: Mul<Rhs=Self> {
/// Product of an empty sequence, i.e. "one".
fn neutral_element() -> Self;
} |
This comment has been minimized.
This comment has been minimized.
|
@aturon I wasn't even aware of the differences in their definition. What I meant by num's Zero and std's Zero are not compatible, is simply that if my crate requires |
This comment has been minimized.
This comment has been minimized.
|
@SimonSapin @sfackler I'm not sure I see the appeal of defining these traits so narrowly, or in @bluss I see. So, in general, we'd like to handle this by changing |
alexcrichton
removed
the
I-nominated
label
Jun 20, 2016
This comment has been minimized.
This comment has been minimized.
|
Ah one question we did have when discussing this was what to do about overflow. We no longer really have the convenience of "overflows as if you wrote the code locally" so we'll have to define semantics one way or another. |
This comment has been minimized.
This comment has been minimized.
I’ve just realize that this design would allow implementations to use an algorithm different from https://docs.python.org/3/library/math.html#math.fsum But is this desirable? It looks like that algorithm allocates memory, so there’s a run-time cost to get that accuracy. This may be a trade-off better left to users to decide. Python itself provides So I think such an accurate sum would be better as a separate API, and it could be on crates.io at least at first. I think that the impls in std of theses traits should simply use This also deals with overflow semantics, doesn’t it? Leave it per-impl, but give a recommendation (and follow it in std). |
This comment has been minimized.
This comment has been minimized.
You can get a significant precision win with just one extra float for which no heap allocation is required (at the cost of more arithmetic operations). But I wouln’t expect the standard library |
This comment has been minimized.
This comment has been minimized.
|
Someone did actually mention the overflow problem. I currently have an open issue at the rfcs repo about checked (and the like) methods for @cuviper suggested:
So we had something like: // within Iterator
fn sum<S>(self) -> Option<S>
where S: Add<Self::Item, Output=S>This would also solve the overflowing problem (although it could return The If for any reason you want to not use the |
This comment has been minimized.
This comment has been minimized.
I would expect Using I don't see a huge difference between Simon's last suggestion and my adjustment to his earlier suggestion in terms of how much special-purpose code needs to get added to the standard library. Is there another possibility, to use a trait-based fold design? trait Foldable<T, R> {
fn initial() -> R;
fn fold(x: T, r: R) -> Option<R>;
}This may allow implementations like use fold_impls::Sum;
fn main() {
let v = vec![1, 2, 3, 4];
println!("Sum: {}", v.fold_with(Sum).unwrap());
} |
This comment has been minimized.
This comment has been minimized.
|
Sorry, no I meant something different and accidentally hit Ctrl while trying to get a newline somewhere, sorry for that. First of all, current feature gated This also means that your fn fold_with<T,R>(&mut self, fold_t: &Foldable<T, R>) -> Option<R>Should actually take ownership of the value. Edit: current fold looks like this: fn fold<B, F>(self, init: B, f: F) -> B where F: FnMut(B, Self::Item) -> B { ... }Furthermore, could you please explain what advantages your Just Sorry, it might just be me not seeing the point there. |
This comment has been minimized.
This comment has been minimized.
I'd be happy if you added the Also how would that:
work if you take fn fold_with<T,R>(&mut self, fold_t: &Foldable<T, R>) -> Option<R>Wouldn't it be more like extern crate something;
fn main() {
let v: Vec<usize> = vec![1,2,3];
// just to have some unified way to get such a structure `new` is used
// because sometimes there might be an internal state to hold (for caching, or whatever reasons)
// if this was not desirable one could simply have `let sum = something::sum::Sum;` I think
let sum = something::sum::Sum::new();
println!("Sum: {}", v.iter().fold_with(&sum).unwrap());
}Edit: I just saw you corrected the |
This comment has been minimized.
This comment has been minimized.
|
Thanks for your comments. @benaryorg The reference to Maybe you're right that an object The potential advantage of the trait is to specify two related things together (both functions of the same trait impl), but maybe it's not worth it. |
This comment has been minimized.
This comment has been minimized.
|
Note that at least in the past when the libs team has discussed these APIs the thought is that if we don't have the ergonomics that we have today then the methods probably aren't worth it. The Along those lines I don't think we want to introduce new methods like The thinking is that using a |
This comment has been minimized.
This comment has been minimized.
|
I'll stick to @SimonSapin's solution then. One can still check for overflows by invoking v.iter().fold(Some(0),|a,&b|a.and_then(|a: usize|a.checked_add(b))) |
This comment has been minimized.
This comment has been minimized.
|
Just to add another use-case of While |
This comment has been minimized.
This comment has been minimized.
|
I agree wholeheartedly with @alexcrichton -- at this point, with this tracking issue, we should focus on landing support for ergonomic |
This comment has been minimized.
This comment has been minimized.
|
The libs team discussed this issue during triage yesterday and the decision was to stabilize. We realize that the FCP for this issue was pretty short, however, so please comment with any objections you might have! We're very willing to backport an un-stabilization for the few APIs we have this cycle. Specifically, we're thinking of stabilizing the |
This comment has been minimized.
This comment has been minimized.
|
I feel that something monoid-identity like might not be a good match for What is really desired for |
alexcrichton
added a commit
to alexcrichton/rust
that referenced
this issue
Jun 28, 2016
alexcrichton
referenced this issue
Jun 28, 2016
Merged
std: Stabilize APIs for the 1.11.0 release #34530
This comment has been minimized.
This comment has been minimized.
|
@alexcrichton So, stabilizing without any of the API changes proposed here? |
This comment has been minimized.
This comment has been minimized.
|
@SimonSapin No, we're moving to the |
aturon commentedAug 12, 2015
We currently have
ZeroandOnetraits that are meant to work withAddandMuland support iterator operations likesum, as well as the currentstep_byAPI.It would be good to have a more comprehensive vision for this kind of trait before stabilization; an RFC would be ideal.