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

RFC: Add syntaxic sugar for struct expressions fields from variables of the same name #306

Closed
rust-highfive opened this issue Sep 24, 2014 · 10 comments
Assignees

Comments

@rust-highfive
Copy link

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:

StructName { field_name: new_variable_name }

But when the desired variable name is the same as the field name, it does not need to be repeated:

let MyStruct { name } = struct_value;
do_something_with(name);

This proposal is to add similar syntax sugar for struct expressions:

let name = …;
let struct_value = MyStruct { name };

… would be the same as:

let name = …;
let struct_value = MyStruct { name: name };

CC: @eddyb

@SimonSapin
Copy link
Contributor

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. for x in Foo { x } { }, but the grammar was changed since then (I think?) so that the desugared form of this would not be valid either.

fn main() {
    struct Foo { x: () }
    impl Iterator<()> for Foo {
        fn next(&mut self) -> Option<()> { None }
    }
    let x = ();
    for i in Foo { x: x } { }
}
a.rs:7:21: 7:22 error: expected one of `;`, `}`, found `:`
a.rs:7     for i in Foo { x: x } { }

CC @nick29581

@theemathas
Copy link

Is this still unambiguous with type ascription?

@chris-morgan
Copy link
Member

@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.

@pnkfelix
Copy link
Member

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.

@pnkfelix pnkfelix self-assigned this Jan 26, 2016
@SimonSapin
Copy link
Contributor

Sounds great, thanks for championing this Felix!

@KalitaAlexey
Copy link

In feature we will have feature Rename variable which will break code or should be insert field name.

@pnkfelix
Copy link
Member

@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

@petrochenkov
Copy link
Contributor

@pnkfelix
Any updates? Do you still plan to write an RFC?
Since implementation is trivial, it's only a matter of decision.

@pnkfelix
Copy link
Member

@petrochenkov I've been distracted. Anyone else is free to draft up an RFC; I won't be upset if that happens.

withoutboats pushed a commit to withoutboats/rfcs that referenced this issue Jan 15, 2017
@petrochenkov
Copy link
Contributor

This is implemented and stabilized in rust-lang/rust#39761
ping @SimonSapin @pnkfelix for closing

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

7 participants