@@ -76,11 +76,12 @@ Languages from the 70ies managed to get this right, Rust somehow regressed on th
76
76
77
77
### A Solution
78
78
79
- What Rust could have done instead:
79
+ What Rust should have done instead:
80
80
81
81
- Function calls and struct/enum initializers use ` () ` , not a mix of ` {} ` and ` () ` .
82
82
- 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.)
84
85
- Shorthand notation or positional arguments? Pick one.
85
86
86
87
Adapting the example code from above, the code would look like this:
@@ -103,7 +104,9 @@ fn main() {
103
104
}
104
105
```
105
106
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?
107
110
108
111
Example:
109
112
@@ -115,13 +118,11 @@ Two points have to be considered here:
115
118
116
119
1 . Reducing chances of mix-ups:
117
120
- 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?
119
122
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?
123
124
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
125
126
126
127
This means that named parameters simply act like another scope in which identifiers are looked up.
127
128
@@ -134,9 +135,9 @@ The danger with this approach is that changing the name of a named parameter can
134
135
135
136
It also means that variables with the same name as a parameter name cannot be assigned within a function call.
136
137
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
138
139
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.
140
141
141
142
// variable assigment inside a function argument list,
142
143
// 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
147
148
// without the callsite being updated, it becomes a compilation failure:
148
149
someFunction(.a = 23)
149
150
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
152
152
153
153
Inside a functions argument list, the first level of ` = ` use is always a named parameter and never a
154
154
variable assignment, even if some variable ` a ` would be in scope.[ ^ 2 ]
0 commit comments