RFC: Anonymous Associated Types by knickish · Pull Request #3342 · rust-lang/rfc...
source link: https://github.com/rust-lang/rfcs/pull/3342
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
knickish commented Nov 2, 2022 •
Create new anonymous types in trait impl blocks to use as associated types. Short example:
pub trait MyTrait{
type Fields;
}
pub struct MyStruct{
field1: usize
}
impl MyTrait for MyStruct{
type Fields = enum { Field1(usize) };
}
added the T-lang Relevant to the language subteam, which will review and decide on the RFC. label
rodrimati1992 commented Nov 4, 2022 •
Overall, I feel like the intended semantics are too unclear. Are these associated types supposed to be constructible outside of the impl block? Are they opaque outside of the impl block? What does this RFC accomplish that associated type impl Trait does not:
The |
Contributor
clarfonthey commented Nov 4, 2022
Hmm, at least from my gut reaction, this feature seems a little limited. I find it less common that I'll make an associated type that's just deriving things, and more likely that I'll be implementing an entire trait for it, and there's no real way to do that under this model. Another important thing to note is whether we should limit this idea of anonymous types to other places in the code too. For example, people have mentioned anonymous type fields for parity with a lot of C-style struct definitions, where you could define a type just for a single field. I feel like whatever syntax and decisions we go with here should also be applicable to cases like that, and maybe there's definitely a lot of unexplored potential for anonymous types across the language. |
imho so far, the only Rust types that behave like that are tuples, and arguably arrays, pointers, references, and slices. |
Author
knickish commented Nov 5, 2022
Author
knickish commented Nov 5, 2022
Sorry about that, hit close button by accident. This does work very well for hiding the internals, but that feature has been on nightly since 2019 and has a long way to go it seems like, and is a much larger feature |
Author
knickish commented Nov 5, 2022
I'll try to add some better examples of ways to impl traits for the in trait structs. I agree that more anonymous structs would be nice, but I think that this would fit in fine with that regardless and is a good first step in that direction. |
rodrimati1992 commented Nov 5, 2022 •
impl Trait in traits is also needed for async methods in traits, so I wouldn't be surprised if it's stabilized before Anonymous Associated Types are stabilized(assuming that this RFC is accepted). |
ExpHP commented Nov 9, 2022 •
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK