-
Notifications
You must be signed in to change notification settings - Fork 13.3k
[WIP] Document field init shorthand #39096
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2757,6 +2757,19 @@ let base = Point3d {x: 1, y: 2, z: 3}; | |
Point3d {y: 0, z: 10, .. base}; | ||
``` | ||
|
||
#### Struct field init shorthand | ||
|
||
When initializing a data structure (struct, enum, union) with named fields, allow writing `fieldname` as a shorthand for `fieldname: fieldname`. This allows a compact syntax for initialization, with less duplication. | ||
|
||
In the initializer for a `struct` with named fields, a `union` with named fields, or an enum variant with named fields, accept an identifier `field` as a shorthand for `field: field`. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. "a union" -> "an union" There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As an English speaker, "a union" sounds write to me. Silly little quirks in this language... There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @GuillaumeGomez, There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. English is way too complicated... There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @GuillaumeGomez, There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's always a simple rule, but there are a lot of them, which makes the whole complicated. |
||
|
||
Example: | ||
|
||
``` | ||
let a = SomeStruct { field1, field2: expression, field3 }; | ||
let b = SomeStruct { field1: field1, field2: expression, field3: field3 }; | ||
``` | ||
|
||
### Block expressions | ||
|
||
A _block expression_ is similar to a module in terms of the declarations that | ||
|
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.
"intializing" -> initialize
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.
This sentence doesn't read write to me at all. I would replace all of
a data structure (struct, enum, union) with named fields
withnamed fields
.I would also add a sentence saying that a variable of the name of the field also has to be in scope.
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.
It seems wrong to me.
I would write:
Initialization of a data structure can be simplified if fields of the data structure are initialized with variables which has same names as the fields.
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.
I agree with @KalitaAlexey 's suggestion.