-
Notifications
You must be signed in to change notification settings - Fork 13.3k
Add the identity function as core::convert::identity #47562
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
Thanks for the pull request, and welcome! The Rust team is excited to review your changes, and you should hear from @sfackler (or someone else) soon. If any changes to this PR are deemed necessary, please add them as extra commits. This ensures that the reviewer can see what has changed since they last reviewed the code. Due to the way GitHub handles out-of-date commits, this should also make it reasonably obvious what issues have or haven't been addressed. Large or tricky changes may require several passes of review and changes. Please see the contribution instructions for more information. |
I'm wary of adding unstable stuff to the prelude, feels like it could still cause clashes with other glob imports. (also, shouldn't this go through an rfc first?) |
Any example you have in mind? AFAIK any import or function definition that the user makes, glob or not will override anything in the prelude..
I can do that if you want... But the addition is rather minor - there's not much technical to discuss - only if we want this in libcore or not... And there's precedent for this in other languages like Haskell, Scala, Java, Idris |
@Centril the interaction between two glob imports is the thing here. Adding stuff to the stdlib usually goes through an RFC, however trivial. This goes double for things in the prelude. It's sometimes fine to add stuff as a feature-gated API before making the RFC but you need some justification for that. |
@Manishearth Alright; I'll write up an RFC. I'll leave this PR open meanwhile so that we can use it if the RFC gets accepted. |
Probably should close this and reopen if the RFC merges. Unless we intend to get it merged soon, it shouldn't stay open. |
That's fair. Expect and RFC filed by the end of the day. |
The prelude change seems good to have an rfc for? New minor features do not otherwise need an rfc, I think this change fits well inside what has been added as unstable without rfc recently. |
@bluss Yeah. Tho I think that in this case the function is much less useful outside the prelude. |
Once the tests pass, this should be ready to go. I've added tests for the |
src/libcore/convert.rs
Outdated
/// let filtered = iter.filter_map(identity).collect::<Vec<_>>(); | ||
/// assert_eq!(vec![1, 3], filtered); | ||
/// ``` | ||
#[unstable(feature = "convert_id", issue = "0")] |
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.
Should be issue = "53500"
now.
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.
Cheers; you always miss something :D
@bors r+ |
📌 Commit 86641d9 has been approved by |
Add the identity function as core::convert::identity ## New notes This implements rust-lang/rfcs#2306 (see #53500). ## Old notes (ignore this in new reviews) Adds the identity function `fn id<T>(x: T) -> T { x }` to core::convert and the prelude. Some motivations for why this is useful are explained in the doc tests. Another is that using the identity function instead of `{ x }` or `|x| x` makes it clear that you intended to use an identity conversion on purpose. The reasoning: + behind adding this to `convert` and not `mem` is that this is an identity *conversion*. + for adding this to the prelude is that it should be easy enough to use that the ease of writing your own identity function or using a closure `|x| x` doesn't overtake that. I've separated this out into two feature gates so that the addition to the prelude can be considered and stabilized separately. cc @bluss
☀️ Test successful - status-appveyor, status-travis |
New notes
This implements rust-lang/rfcs#2306 (see #53500).
Old notes (ignore this in new reviews)
Adds the identity function
fn id<T>(x: T) -> T { x }
to core::convert and the prelude.Some motivations for why this is useful are explained in the doc tests.
Another is that using the identity function instead of
{ x }
or|x| x
makes it clear that you intended to use an identity conversion on purpose.The reasoning:
convert
and notmem
is that this is an identity conversion.|x| x
doesn't overtake that.I've separated this out into two feature gates so that the addition to the prelude can be considered and stabilized separately.
cc @bluss