1
1
---
2
2
RFC : 0002
3
3
Author : Jason Shirk
4
- Status : Draft
4
+ Status : Draft-Accepted
5
5
Area : Splatting
6
6
Comments Due : 3/31/2016
7
+ Edits : Joey Aiello
7
8
---
8
9
9
10
# Generalized Splatting
@@ -47,7 +48,7 @@ $addTypeParams = @{
47
48
MemberType = 'NoteProperty'
48
49
Value = 42
49
50
}
50
- Update-TypeData @addTypeParams
51
+ Update-TypeData @addTypeParams
51
52
```
52
53
53
54
This works, but feels a bit messy because of the need for a variable,
@@ -60,7 +61,7 @@ $PSBoundParameters.Remove('SomeExtraParam')
60
61
Command @PSBoundParameters
61
62
```
62
63
63
- This proposal suggesst a syntax that improves this scenario as well.
64
+ This proposal suggests a syntax that improves this scenario as well.
64
65
65
66
## Specification
66
67
@@ -118,7 +119,7 @@ Get-ChildItem @$myArgs
118
119
```
119
120
120
121
The above example would fail with a "parameter not found" because of the 'ExtraStuff' key.
121
- Here is a possible syntax to allow the above without resulting in an error:
122
+ Here is a possible syntax to allow the above without resulting in an error:
122
123
123
124
``` PowerShell
124
125
$myArgs = @{ Path = $pwd; ExtraStuff = 1234 }
@@ -130,6 +131,15 @@ If '@' is the splatting operator,
130
131
adding the '?' is suggestive of being more permissive,
131
132
much like the C# '?.' member access operator.
132
133
134
+ If parameter values are passed explicitly in addition to the relaxed splatting operator,
135
+ those values would take precedence over anything in the splatted hashtable:
136
+
137
+ ``` PowerShell
138
+ $myArgs = @{ Path = C:\foo; ExtraStuff = 1234 }
139
+ Get-ChildItem @?$myArgs -Path C:\bar
140
+ # Lists the children of C:\bar
141
+ ```
142
+
133
143
### Splatting in method invocations
134
144
135
145
Today, named arguments are only supported when calling commands,
@@ -160,7 +170,7 @@ and via splatting in the same invocation:
160
170
# Must be an error, parse time or runtime, because startIndex
161
171
# is specified positionally and via splatting.
162
172
$subStringArgs = @{startIndex = 2}
163
- $str.SubString(2 , @$subStringArgs)
173
+ $str.SubString(3 , @$subStringArgs)
164
174
```
165
175
166
176
Multiple splatted arguments are not allowed:
@@ -177,29 +187,58 @@ The splatted argument must be last.
177
187
$str.SubString(@@{length=2}, 2)
178
188
```
179
189
180
- ### Splatting in switch cases
190
+ ## Alternate Proposals and Considerations
181
191
182
- It can be awkward to match multiple conditions with a single switch statement.
183
- For example:
192
+ ### Relaxed splatting in method invocations
193
+
194
+ Initially, we wanted to support relaxed splatting for invocation of .NET methods.
195
+ In this case, the ` 3 ` value would override the value in ` $subStringArgs ` :
184
196
185
197
``` PowerShell
186
- switch ($color) {
187
- { $_ -eq 'Red' -or $_ -eq 'Blue' -or $_ -eq 'Green' } { $true }
188
- default { $false }
189
- }
198
+ # This will not result in an error,
199
+ # and the substring will be of length 3.
200
+ $subStringArgs = @{startIndex = 2 }
201
+ $str.SubString(3, @?$subStringArgs)
190
202
```
191
203
192
- With splatting, this can be simplified:
204
+ However, some situations may make it ambiguous or unclear as to which overload you're invoking.
193
205
194
- ``` PowerShell
195
- switch ($color) {
196
- @@('Red','Blue','Green') { $true }
197
- default { $false }
206
+ While not a good practice for API design, if the third overload below is added at a later date,
207
+ the meaning of the PowerShell will change when using relaxed splatting.
208
+
209
+ ``` csharp
210
+ class foo {
211
+ void static bar (int a , string b );
212
+ void static bar (int a , string b , int c );
213
+ // this third overload may be added at a later date
214
+ void static bar (int d , int a , string b , int c );
198
215
}
199
216
```
200
217
218
+ ``` PowerShell
219
+ $params = @{a = 1; b = '2'; c = 3}
220
+
221
+ [foo]::bar(0, @?$params)
222
+ ```
223
+
224
+ ### Postfix operator
225
+
226
+ The use of a sigil is not always well received.
227
+ This proposal nearly considers '@' a prefix unary operator,
228
+ but it doesn't quite specify it as such.
229
+
230
+ A postfix operator is another possiblity and would look less like a sigil.
231
+ This idea was rejected because, when reading a command invocation from left to right,
232
+ it's important to understand how a hash literal is to be used.
233
+ The sigil makes it clear a hash literal is really specifying command arguments.
234
+ Furthermore, the sigil simplifies the analysis required for good parameter completion,
235
+ and does not require a complete expression to begin providing parameter name completion.
236
+
201
237
### Modifying hashtables for splatting
202
238
239
+ > The following features could be useful in the scenarios described above,
240
+ > but they should be written about in more detail in another RFC.
241
+
203
242
Sometimes it is useful to provide a 'slice' of a hashtable,
204
243
e.g. you want to remove or include specific keys.
205
244
The Add/Remove methods on a hashtable work, but can be verbose.
@@ -208,7 +247,7 @@ This proposal suggests overloading the '+' and '-' operators to provide a hashta
208
247
``` PowerShell
209
248
Get-ChildItem @$($PSBoundParameters - 'Force') # Splat all parameters but 'Force'
210
249
Get-ChildItem @$($PSBoundParameters - 'Force','WhatIf') # Splat all parameters but 'Force' and 'WhatIf'
211
- Get-ChildItem @$($PSBOundParameters + 'LiteralPath','Path') # Only splat 'LiteralPath' and 'Path'
250
+ Get-ChildItem @$($PSBoundParameters + 'LiteralPath','Path') # Only splat 'LiteralPath' and 'Path'
212
251
```
213
252
214
253
Today, PowerShell supports "adding" two hashtables with the '+' operator,
@@ -227,25 +266,38 @@ When using '-', the result will exclude all keys from the right hand side.
227
266
In either case,
228
267
it is not an error to specify a key in the right hand side operand that is not present in the left hand side.
229
268
230
- ## Alternate Proposals and Considerations
231
-
232
- ### Slicing operators
233
-
234
269
The suggested use of '+' and '-' is perhaps surprising
235
270
even though they correspond to Add and Remove, respectively.
236
271
The actual operation is also similar to a union or intersection,
237
272
so other operators should be considered, perhaps bitwise operators
238
- like '-bnot' and '-bor', or maybe new general purpose set operators.
273
+ like '-bnot' and '-bor', or maybe new general purpose set operators.
239
274
240
- ### Postfix operator
275
+ ---------------
241
276
242
- The use of a sigil is not always well received.
243
- This proposal nearly considers '@' a prefix unary operator,
244
- but it doesn't quite specify it as such.
277
+ ## PowerShell Committee Decision
245
278
246
- A postfix operator is another possiblity and would look less like a sigil.
247
- This idea was rejected because, when reading a command invocation from left to right,
248
- it's important to understand how a hash literal is to be used.
249
- The sigil makes it clear a hash literal is really specifying command arguments.
250
- Furthermore, the sigil simplifies the analysis required for good parameter completion,
251
- and does not require a complete expression to begin providing parameter name completion.
279
+ ### Voting Results
280
+
281
+ Joey Aiello: Accept
282
+
283
+ Bruce Payette: Accept
284
+
285
+ Steve Lee: Accept
286
+
287
+ Hemant Mahawar: Accept
288
+
289
+ Dongbo Wang: Accept
290
+
291
+ Kenneth Hansen: Accept
292
+
293
+ ### Majority Decision
294
+
295
+ Committee agrees that this is the above features would be useful to have in splatting.
296
+ However, we do not currently have a plan to implement any of this,
297
+ so it can be picked up by a member of the community.
298
+
299
+ Also, it would be useful to build a new RFC for hashtable manipulation per the alternate considerations.
300
+
301
+ ### Minority Decision
302
+
303
+ N/A
0 commit comments