-
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
RFC: Add syntaxic sugar for struct expressions fields from variables of the same name #306
Comments
This was discussed in https://github.com/rust-lang/meeting-minutes/blob/master/weekly-meetings/2014-02-11.md#binding-in-struct-patterns There were some concerns about ambiguities in e.g. fn main() {
struct Foo { x: () }
impl Iterator<()> for Foo {
fn next(&mut self) -> Option<()> { None }
}
let x = ();
for i in Foo { x: x } { }
}
CC @nick29581 |
Is this still unambiguous with type ascription? |
@theemathas: it is still unambiguous; the full form of struct initialisation is defined appropriately so that type ascription works, and the additional, simpler form that this permits cannot have any colons (it’s commas and identifiers only), so there is no interaction with the type ascription feature. |
So, this feature was suggested (rust-lang/rust#11990) and then prototyped ( rust-lang/rust#11994 ) back in February 2014 The team collectively declined to adopt the feature for Rust 1.0 : rust-lang/rust#11990 (comment) (again, back in Febuary 2014). But then in June 2014, we accepted RFC 92, that restricted the use of struct literals to only appear in certain expression positions.
I believe that with the restriction specified by RFC 92, we could readily add this feature to the language backwards compatibly; that is, the parsing ambiguity issues are no longer present, I think.
I'm going to assign this to myself as a reminder to try to draft up a formal RFC for it at some point in the (hopefully near) future. |
Sounds great, thanks for championing this Felix! |
In feature we will have feature Rename variable which will break code or should be insert field name. |
@KalitaAlexey I infer you are talking about an IDE feature. I think the language design should not hinder itself on how IDE features are to be implemented |
@pnkfelix |
@petrochenkov I've been distracted. Anyone else is free to draft up an RFC; I won't be upset if that happens. |
This is implemented and stabilized in rust-lang/rust#39761 |
Issue by SimonSapin
Sunday Feb 02, 2014 at 12:29 GMT
For earlier discussion, see rust-lang/rust#11990
This issue was labelled with: A-parser, B-RFC in the Rust repository
The general syntax for a struct pattern is:
But when the desired variable name is the same as the field name, it does not need to be repeated:
This proposal is to add similar syntax sugar for struct expressions:
… would be the same as:
CC: @eddyb
The text was updated successfully, but these errors were encountered: