Uh oh!
There was an error while loading. Please reload this page.
Implement field shorthands in struct literal expressions. - #11994
Conversation
huonw
commented
Feb 2, 2014
This is a (nice) syntax change, and so should get some discussion of some sort. |
alexcrichton
commented
Feb 2, 2014
I'm surprised how easy that was to add! I've added this to the meeting agenda (I'll put my vote in favor of it though). |
alexcrichton
commented
Feb 2, 2014
One possible ambiguity issue that we've come up with on IRC: for x inFoo{ a }{ b } |
huonw
commented
Feb 2, 2014
Maybe we could just allow omitting the field name, but not the colon: let(some_field, other_field) = (1,2);for x inFoo{: some_field,: other_field }{ ...}... but that looks rather peculiar. |
eddyb
commented
Feb 3, 2014
@huonw the ambiguity only shows up with one-field structures. How often have you seen |
huonw
commented
Feb 4, 2014
We'll need a few tests for the comma thing too. |
SimonSapin
commented
Feb 5, 2014
Even though it leaves a distasteful inconsistency in the language, |
alexcrichton
commented
Feb 11, 2014
We decided in today's meeting that due to ambiguities and some hygiene related concerns that we don't want this for now. Perhaps in rust 2.0! |
SimonSapin
commented
Feb 11, 2014
Sad panda. Why as late as 2.0, isn’t it backward-compatible? |
alexcrichton
commented
Feb 11, 2014
Ah I should clarify by 2.0 I mean "after 1.0" perhaps. We didn't concretely decide for a time for something like this to come in, we decided that now isn't the time for it though. |
huonw
commented
Feb 11, 2014
(Sorry for the spam: phone touchscreen leads to "pocket commenting" :( ) |
ben0x539
commented
Feb 11, 2014
Wouldn't be an ambiguity if we had |
SimonSapin
commented
Feb 11, 2014
Too bad. I find myself often writing code ending like this: Where each field has be computed earlier. This sugar would make it much nicer. As to the ambiguities, they only occur in cases where (IMO) it’s rare to have a literal struct expression. (I wanted to write up some arguments that didn’t seem to appear in the meeting notes, but I’ll now stop beating this dead horse.) |
pnkfelix
commented
Feb 13, 2014
@SimonSapin This seems trivial to work around for now via #[feature(macro_rules)];macro_rules! struct_fields {($Struct:ident $($field:ident),+ )
=> { $Struct { $($field: $field),+ }};}structSchemeRelativeUrl{userinfo: ~str,host:[u8, ..4],port:u32,path: ~str}fnmain(){let userinfo = ~"hi";let host = [1,2,3,4];let port = 56;let path = ~"/root/"; let s = struct_fields!( SchemeRelativeUrl userinfo, host, port, path ); println!("s:{:?}", s);} |
SimonSapin
commented
Feb 13, 2014
@pnkfelix This’ll help, thanks. I didn’t think of using a macro. However this doesn’t as easily cover cases where only some fields are based on a variable of the same name: Still, sugar baked into the language is always nicer than a macro you have to define :) |
huonw
commented
Feb 13, 2014
macro_rules! struct_field {($Struct:ident $($short:ident),*; $($long:ident: $long_e: expr)*) => {
$Struct { $($short: $short,)* $($long: $long_e),*}}}// ...struct_fields!(Url scheme, query, fragment; scheme_data:OtherSchemeData(scheme_data)) |
SimonSapin
commented
Feb 13, 2014
Nice! Thanks @huonw. Is the |
huonw
commented
Feb 13, 2014
It doesn't backtrack (I haven't compiled that so there may actually need to be more disambiguation necessary :( ). |
eddyb
commented
Oct 22, 2016
I'm genuinely curious to know how little needs to be updated here for it to work for #37340. |
a2fc2b0 to
4b26bdeCompareKalitaAlexey
commented
Oct 22, 2016
@eddyb I see that in tests you follow the order of fields in a struct. Could you, please, some tests for case like: structFoo{x:i32,y:i32,}let x = 5;let y = 6;let foo = Foo{ y, x }; |
petrochenkov
commented
Oct 22, 2016
Ha, this seems to parse shortcuts for numeric fields: Not sure if this is bad, but this is avoided in pattern parsing by calling |
petrochenkov
commented
Oct 22, 2016
A feature gate is also probably needed now, it's not 2014 anymore :( |
4b26bde to
6e4ab6aCompare6e4ab6a to
237bd2dCompare237bd2d to
9908711Comparenrc
commented
Oct 27, 2016
@bors: r+ |
bors
commented
Oct 27, 2016
📌 Commit 9908711 has been approved by |
bors
commented
Oct 27, 2016
Implement field shorthands in struct literal expressions. Implements #37340 in a straight-forward way: `Foo { x, y: f() }` parses as `Foo { x: x, y: f() }`. Because of the added `is_shorthand` to `ast::Field`, this is `[syntax-breaking]` (cc @Manishearth). * [x] Mark the fields as being a shorthand (the exact same way we do it in patterns), for pretty-printing. * [x] Gate the shorthand syntax with `#![feature(field_init_shorthand)]`. * [x] Don't parse numeric field as identifiers. * [x] Arbitrary field order tests.
bors
commented
Oct 27, 2016
[`question_mark`]: also trigger on `return` statements This fixes the false negative mentioned in rust-lang#11993: the lint only used to check for `return` expressions, and not a statement containing a `return` expression (doesn't close the issue tho since there's still a useful suggestion that we could make, which is to suggest `.ok_or()?`/`.ok_or_else()?` for `else { return Err(..) }`) changelog: [`question_mark`]: also trigger on `return` statements
Implements #37340 in a straight-forward way:
Foo { x, y: f() }parses asFoo { x: x, y: f() }.Because of the added
is_shorthandtoast::Field, this is[syntax-breaking](cc @Manishearth).#![feature(field_init_shorthand)].