Skip to content

Commit adce49a

Browse files
authored
fixup up struct init
1 parent f1b594e commit adce49a

File tree

1 file changed

+12
-12
lines changed

1 file changed

+12
-12
lines changed

_languages/rust-struct-initializer-mistake.md

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -76,11 +76,12 @@ Languages from the 70ies managed to get this right, Rust somehow regressed on th
7676

7777
### A Solution
7878

79-
What Rust could have done instead:
79+
What Rust should have done instead:
8080

8181
- Function calls and struct/enum initializers use `()`, not a mix of `{}` and `()`.
8282
- Have *one* ruleset that all those invocation follow.
83-
- Use `=` for passing actual values as named arguments, such that everything "just works" if default parameters are added in the future.
83+
- Use `=` for passing actual values as named arguments, such that everything "just works" if default parameters were added in the future.
84+
(See the [Appendix](#appendix-a-detailed-look-at-the-role-of) for an evaluation of available options.)
8485
- Shorthand notation or positional arguments? Pick one.
8586

8687
Adapting the example code from above, the code would look like this:
@@ -103,7 +104,9 @@ fn main() {
103104
}
104105
```
105106

106-
#### How to Distinguish between a variable assigment and a named parameter use inside a function invocation?
107+
### Appendix: A Detailed Look at the Role of `=`
108+
109+
#### How to distinguish between a variable assigment and a named parameter use inside a function invocation?
107110

108111
Example:
109112

@@ -115,13 +118,11 @@ Two points have to be considered here:
115118

116119
1. Reducing chances of mix-ups:
117120
- Frequency: Are variable assignments within function calls or function calls with named parameters projected to be used more often?
118-
- Intuitivity: How can the syntax be distributed to those two use-cases such that the configuration makes sense from a user point-of-view?
121+
- Intuitivity: How can the syntax be distributed to those two use-cases such that the choice makes intuitively sense from a user point-of-view?
119122
2. Reducing the harm from mix-ups:
120-
- Can code that looks correct be interpreted unexpectedly? I. e.
121-
122-
In general, there are multiple options:
123+
- Can code change meaning unexpectedly, e. g. when function parameters are renamed?
123124

124-
##### Try to use the same syntax for both variable assigments and named parameters
125+
##### Option 1: Try to use the same syntax for both variable assigments and named parameters
125126

126127
This means that named parameters simply act like another scope in which identifiers are looked up.
127128

@@ -134,9 +135,9 @@ The danger with this approach is that changing the name of a named parameter can
134135

135136
It also means that variables with the same name as a parameter name cannot be assigned within a function call.
136137

137-
##### Let variable assignments use the "good" syntax and give named parameters some workaround syntax
138+
##### Option 2: Let variable assignments use the "good" syntax and give named parameters some "workaround" syntax
138139

139-
In this example, the orkaround syntax for named parameters is a `.` prefixed to the parameter name.
140+
In this example, the workaround syntax for named parameters is a `.`, prefixed to the parameter name.
140141

141142
// variable assigment inside a function argument list,
142143
// only works if assignment returns the assigned value
@@ -147,8 +148,7 @@ In this example, the orkaround syntax for named parameters is a `.` prefixed to
147148
// without the callsite being updated, it becomes a compilation failure:
148149
someFunction(.a = 23)
149150

150-
151-
##### Let named parameters use the "good" syntax and give variable assignments some workaround syntax
151+
##### Option 3: Let named parameters use the "good" syntax and give variable assignments some "workaround" syntax
152152

153153
Inside a functions argument list, the first level of `=` use is always a named parameter and never a
154154
variable assignment, even if some variable `a` would be in scope.[^2]

0 commit comments

Comments
 (0)