Skip to content

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

Merged
merged 12 commits into from
Aug 20, 2018

Conversation

Centril
Copy link
Contributor

@Centril Centril commented Jan 19, 2018

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

@rust-highfive
Copy link
Contributor

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.

@kennytm kennytm added T-libs-api Relevant to the library API team, which will review and decide on the PR/issue. S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Jan 19, 2018
@Manishearth
Copy link
Member

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?)

@Centril
Copy link
Contributor Author

Centril commented Jan 19, 2018

I'm wary of adding unstable stuff to the prelude, feels like it could still cause clashes with other glob imports.

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..

(also, shouldn't this go through an rfc first?)

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

@Manishearth
Copy link
Member

@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.

@Centril
Copy link
Contributor Author

Centril commented Jan 19, 2018

@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.

@Manishearth
Copy link
Member

Probably should close this and reopen if the RFC merges. Unless we intend to get it merged soon, it shouldn't stay open.

@Centril
Copy link
Contributor Author

Centril commented Jan 19, 2018

That's fair. Expect and RFC filed by the end of the day.

@bluss
Copy link
Member

bluss commented Jan 19, 2018

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.

@Centril
Copy link
Contributor Author

Centril commented Jan 19, 2018

@bluss Yeah. Tho I think that in this case the function is much less useful outside the prelude.

@Centril Centril changed the title Add the identity function as core::convert::id (+ to prelude) Add the identity function as core::convert::identity Aug 19, 2018
@Centril Centril reopened this Aug 19, 2018
@Centril Centril changed the title Add the identity function as core::convert::identity [WIP] Add the identity function as core::convert::identity Aug 19, 2018
@Centril Centril changed the title [WIP] Add the identity function as core::convert::identity Add the identity function as core::convert::identity Aug 19, 2018
@Centril
Copy link
Contributor Author

Centril commented Aug 19, 2018

Once the tests pass, this should be ready to go.

I've added tests for the rustc_const_unstable(..) bit as well.

/// let filtered = iter.filter_map(identity).collect::<Vec<_>>();
/// assert_eq!(vec![1, 3], filtered);
/// ```
#[unstable(feature = "convert_id", issue = "0")]
Copy link
Member

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.

Copy link
Contributor Author

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

@oli-obk
Copy link
Contributor

oli-obk commented Aug 20, 2018

@bors r+

@bors
Copy link
Collaborator

bors commented Aug 20, 2018

📌 Commit 86641d9 has been approved by oli-obk

@bors bors added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Aug 20, 2018
@bors
Copy link
Collaborator

bors commented Aug 20, 2018

⌛ Testing commit 86641d9 with merge bf1e461...

bors added a commit that referenced this pull request Aug 20, 2018
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
@bors
Copy link
Collaborator

bors commented Aug 20, 2018

☀️ Test successful - status-appveyor, status-travis
Approved by: oli-obk
Pushing bf1e461 to master...

@bors bors merged commit 86641d9 into rust-lang:master Aug 20, 2018
@Centril Centril deleted the feature/core_convert_id branch August 20, 2018 11:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. T-libs-api Relevant to the library API team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

9 participants