-
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
Add support for use Trait::func
#3591
base: master
Are you sure you want to change the base?
Conversation
From The Rust Programming Language section 7.4:
We have been encouraged for a long time to always import the parent module when calling the function to distinguish locally defined functions and imported functions. However, the syntax sugar suggested in this RFC contradicts to this convention. I think it is more appropriate to discuss this convention in the RFC to make it more clear how we should use this feature. :) |
I'd like to reiterate here what I said on #1995 Many libraries define free functions for constructors to mathy types, like vectors. Some examples: It would be nice if instead of having to create wrapper functions, the user (or the lib author with This is some additional motivation that could go into the motivation section. |
That being said, I noticed that this RFC doesn't talk about importing inherent methods, which I think should be addressed. |
I discussed that in the future work section. |
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.
The RFC text uses the term “method” where it should be using the term “associated function”.
- An associated function is a function defined in an
impl
or declared in atrait
. (reference) - A method is an associated function that has a
self
parameter (and therefore may be used in.func_name()
method-call syntax). (reference)
I recommend that this be corrected to avoid creating confusion. In particular, the central use case of this feature is importing associated functions that are less concise to call because they are not methods, such as Default::default
.
use Trait::method
use Trait::func
Apologies for the delay, wasn't able to work up motivation to work on this again until today. Thanks for the review! |
```rust | ||
use Trait::item as m; | ||
``` | ||
occurs, a new item `m` is made available in the value namespace of the current module. Any attempts to use this item are treated as using the associated item explicitly qualified. `item` must be either an associated function or an associated constant. As always, the `as` qualifier is optional, in which case the name of the new item is identical with the name of the associated item in the trait. In other words, the example: |
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.
Hmm, "value namespace" here surprised me. What happens if item
is actually an associated type?
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.
item
must be either an associated function or an associated constant
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.
See this discussion for motivation for including associated constants, which wasn't in my first draft.
👍 to the motivation here. @rfcbot concern what-about-turbofish One thing I think needs to be addressed in the reference section is what's supposed to happen with generic parameters. If one has trait Trait<A> {
fn method<B>(…);
} and does a
What happens? Can you turbofish it? Which generics are there there? The precedent from enum variants doesn't cover generics on the associated items, so something needs to specified for it. |
This can be inferred directly from the desugaring described in the RFC. If the trait has generics, they must be inferrable in order for |
Just wanted to ping this, is there anything I can do to keep this moving? |
Nominated for lang discussion. |
This is currently implemented on nightly as part of #![expect(incomplete_features)]
#![feature(fn_delegation)]
reuse Default::default;
fn type_name_of_val<T>(_: &T) -> &'static str {
std::any::type_name::<T>()
}
fn main() {
let x: u32 = default();
let y = default::<u64>();
let name = type_name_of_val(&default::<()>);
dbg!(x, y, name);
} |
@rfcbot reviewed
I would very much like to see this, though I am okay waiting for a future RFC once the implementation challenges are worked out.
I would also very much like to see this, but yes it belongs in another RFC. While I would prefer to lift the subtrait/supertrait restriction part of this RFC, it sounds annoying to do so it can wait for the next RFC.
Those errors are bizarre and I agree we should make them better (and as I said, eventually remove them altogether). |
Trying to get outstanding concerns resolved: I think the request by @scottmcm was that it be spelled out in the reference section. I don't see a drawback to including implications like this as subsections, even if they are implied by the earlier text. (The paragraph you have on the restriction on important parent trait functions is an example of this.) |
@rfcbot resolved associated-constants |
@obsgolem Would you add some text elaborating on how trait generics work, so we can get the last concern resolved? |
The reference seems to still only talk about using inference for the generics on such an imported thing, like
and
Is the idea here that something imported like this can only be used inferred, and we block turbofishing it for now? For For |
Ok, with the update to say
@rfcbot resolve what-about-turbofish I do think that people will want to turbofish this in the future, but I'm happy to say that for now they just can't, like we did with APIT, and we can reconsider things later as needed. |
🔔 This is now entering its final comment period, as per the review above. 🔔 |
The final comment period, with a disposition to merge, as per the review above, is now complete. As the automated representative of the governance process, I would like to thank the author for their work and everyone else who contributed. This will be merged soon. |
Rendered
This feature fully supplants rust-lang/rust#73001, allowing you to do things like:
and more.
This is my first RFC, please forgive any missteps I make in the process.
Partially completes #1995.