-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
impl-only-use #2166
Conversation
Just as a reference, Haskell has that: import Foo.Bar.Zoo () -- imports the instances (impls) but not the typeclasses (traits) directly |
We'd want this for inherent methods too, not just traits, right? |
@burdges inherent methods don't need to be imported to be used. |
No need for inherent methods because you never express them in the |
text/0000-impl-only-use.md
Outdated
# 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 |
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.
"Qualifying"
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.
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`.
0d69fad
to
17a50ea
Compare
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 |
Hm, isn't conventionally |
Yes, and here it’s the same: a concrete thing (a trait) you don’t care about. :) |
text/0000-impl-only-use.md
Outdated
# Reference-level explanation | ||
[reference-level-explanation]: #reference-level-explanation | ||
|
||
To be defined. |
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.
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
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.
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).
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.
Good comments, I’ll have them added! :)
Oh please this. I could use this in the |
117cac3
to
4727667
Compare
4727667
to
459606f
Compare
🎉 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. |
Nominating for discussion in the @rust-lang/lang team meeting. |
@rfcbot fcp merge |
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. |
🔔 This is now entering its final comment period, as per the review above. 🔔 |
The final comment period is now complete. |
This RFC has been merged! |
This RFC gives Rust the syntax:
Here,
_
means that we want to import theFooTrait
trait’s impls butnot the trait symbol directly, because we might declare the same symbol
in the module issuing the
use
.Rendered