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

impl-only-use #2166

Merged
merged 4 commits into from
Feb 14, 2018
Merged

impl-only-use #2166

merged 4 commits into from
Feb 14, 2018

Conversation

hadronized
Copy link
Contributor

@hadronized hadronized commented Oct 1, 2017

This RFC gives Rust the syntax:

use foo::FooTrait as _;

Here, _ means that we want to import the FooTrait trait’s impls but
not the trait symbol directly, because we might declare the same symbol
in the module issuing the use.


Rendered

@hadronized
Copy link
Contributor Author

hadronized commented Oct 1, 2017

Just as a reference, Haskell has that:

import Foo.Bar.Zoo () -- imports the instances (impls) but not the typeclasses (traits) directly

@burdges
Copy link

burdges commented Oct 1, 2017

We'd want this for inherent methods too, not just traits, right?

@sfackler
Copy link
Member

sfackler commented Oct 1, 2017

@burdges inherent methods don't need to be imported to be used.

@hadronized
Copy link
Contributor Author

No need for inherent methods because you never express them in the use position. :)

# Guide-level explanation
[guide-level-explanation]: #guide-level-explanation

Qualyfing a `use` with `_` on a trait imports the trait’s `impl`s but not the symbol directly. It’s
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"Qualifying"

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice catch, thanks!

This RFC gives Rust the syntax:

```rust
use foo::FooTrait as _;
```

Here, `_` means that we want to import the `FooTrait` trait’s impls but
not the trait symbol directly, because we might declare the same symbol
in the moduling issuing the `use`.
@Centril
Copy link
Contributor

Centril commented Oct 2, 2017

Neat RFC.

Also related to traits, but perhaps deserving of a separate RFC... How about importing trait methods of the following form as free functions:

trait TheTrait {
    fn notUsingInArgPos2(x: T1, y: T2, ...) -> X<TheTrait>;
}

where X can be something like Result<T, ?>

@egilburg
Copy link

egilburg commented Oct 2, 2017

Hm, isn't conventionally _ used to describe a concrete thing you just don't care about, rather than a template wildcard?

@hadronized
Copy link
Contributor Author

hadronized commented Oct 2, 2017

Yes, and here it’s the same: a concrete thing (a trait) you don’t care about. :)

# Reference-level explanation
[reference-level-explanation]: #reference-level-explanation

To be defined.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use Trait as _ needs to desugar into use Trait as SomeUniqueNameYouCantReferTo (i.e. SomeUniqueNameYouCantReferTo is a "gensym").
With this scheme glob imports/reexports can work properly with such items, i.e. import/reexport them.

mod m {
    pub use Trait as _;

    // `Trait` is in scope
}
use m::*;

// `Trait` is in scope too 

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use NonTrait as _ can work the same way, it will just always be reported as unused import.

extern crate my_crate as _ can work in the same way too (this can be useful for linking-only crates).

Copy link
Contributor Author

@hadronized hadronized Oct 3, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good comments, I’ll have them added! :)

@vitiral
Copy link

vitiral commented Oct 3, 2017

Oh please this. I could use this in the prelude crate so I don't have to alias io::Write vs fmt::Write.

@scottmcm scottmcm added the T-lang Relevant to the language team, which will review and decide on the RFC. label Oct 12, 2017
@ordovicia ordovicia mentioned this pull request Oct 24, 2017
@arcnmx
Copy link

arcnmx commented Oct 24, 2017

🎉 Every couple months I accidentally do this hoping maybe it got implemented.

A small point brought up previously is that this additionally affects rustdoc, and probably requires some special handling/formatting to display without just showing the generated ident.

@nikomatsakis
Copy link
Contributor

Nominating for discussion in the @rust-lang/lang team meeting.

@withoutboats
Copy link
Contributor

@rfcbot fcp merge

@rfcbot
Copy link
Collaborator

rfcbot commented Jan 25, 2018

Team member @withoutboats has proposed to merge this. The next step is review by the rest of the tagged teams:

No concerns currently listed.

Once these reviewers reach consensus, this will enter its final comment period. If you spot a major issue that hasn't been raised at any point in this process, please speak up!

See this document for info about what commands tagged team members can give me.

@rfcbot rfcbot added the proposed-final-comment-period Currently awaiting signoff of all team members in order to enter the final comment period. label Jan 25, 2018
@rfcbot
Copy link
Collaborator

rfcbot commented Jan 28, 2018

🔔 This is now entering its final comment period, as per the review above. 🔔

@rfcbot rfcbot added final-comment-period Will be merged/postponed/closed in ~10 calendar days unless new substational objections are raised. and removed proposed-final-comment-period Currently awaiting signoff of all team members in order to enter the final comment period. labels Jan 28, 2018
@rfcbot
Copy link
Collaborator

rfcbot commented Feb 7, 2018

The final comment period is now complete.

@aturon
Copy link
Member

aturon commented Feb 14, 2018

This RFC has been merged!

Tracking issue

@aturon aturon merged commit 51ee8c1 into rust-lang:master Feb 14, 2018
@hadronized hadronized deleted the impl-only-use branch February 25, 2018 12:59
@dtolnay dtolnay mentioned this pull request Sep 2, 2018
@Centril Centril added A-syntax Syntax related proposals & ideas A-resolve Proposals relating to name resolution. labels Nov 23, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-resolve Proposals relating to name resolution. A-syntax Syntax related proposals & ideas final-comment-period Will be merged/postponed/closed in ~10 calendar days unless new substational objections are raised. T-lang Relevant to the language team, which will review and decide on the RFC.
Projects
None yet
Development

Successfully merging this pull request may close these issues.