@@ -4,6 +4,7 @@ Author: Jason Shirk
4
4
Status : Draft
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,16 @@ 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)
174
+ ```
175
+
176
+ Using the relaxed splatting operator, the ` 3 ` value will override the value in ` $subStringArgs ` :
177
+
178
+ ``` PowerShell
179
+ # This will not result in an error,
180
+ # and the substring will be of length 3.
181
+ $subStringArgs = @{startIndex = 2}
182
+ $str.SubString(3, @?$subStringArgs)
164
183
```
165
184
166
185
Multiple splatted arguments are not allowed:
@@ -177,29 +196,35 @@ The splatted argument must be last.
177
196
$str.SubString(@@{length=2}, 2)
178
197
```
179
198
180
- ### Splatting in switch cases
181
199
182
- It can be awkward to match multiple conditions with a single switch statement.
183
- For example:
200
+ ## Alternate Proposals and Considerations
184
201
185
- ``` PowerShell
186
- switch ($color) {
187
- { $_ -eq 'Red' -or $_ -eq 'Blue' -or $_ -eq 'Green' } { $true }
188
- default { $false }
189
- }
190
- ```
202
+ ### Slicing operators
191
203
192
- With splatting, this can be simplified:
204
+ The suggested use of '+' and '-' is perhaps surprising
205
+ even though they correspond to Add and Remove, respectively.
206
+ The actual operation is also similar to a union or intersection,
207
+ so other operators should be considered, perhaps bitwise operators
208
+ like '-bnot' and '-bor', or maybe new general purpose set operators.
193
209
194
- ``` PowerShell
195
- switch ($color) {
196
- @@('Red','Blue','Green') { $true }
197
- default { $false }
198
- }
199
- ```
210
+ ### Postfix operator
211
+
212
+ The use of a sigil is not always well received.
213
+ This proposal nearly considers '@' a prefix unary operator,
214
+ but it doesn't quite specify it as such.
215
+
216
+ A postfix operator is another possiblity and would look less like a sigil.
217
+ This idea was rejected because, when reading a command invocation from left to right,
218
+ it's important to understand how a hash literal is to be used.
219
+ The sigil makes it clear a hash literal is really specifying command arguments.
220
+ Furthermore, the sigil simplifies the analysis required for good parameter completion,
221
+ and does not require a complete expression to begin providing parameter name completion.
200
222
201
223
### Modifying hashtables for splatting
202
224
225
+ > The following features could be useful in the scenarios described above,
226
+ > but they should be written about in more detail in another RFC.
227
+
203
228
Sometimes it is useful to provide a 'slice' of a hashtable,
204
229
e.g. you want to remove or include specific keys.
205
230
The Add/Remove methods on a hashtable work, but can be verbose.
@@ -208,7 +233,7 @@ This proposal suggests overloading the '+' and '-' operators to provide a hashta
208
233
``` PowerShell
209
234
Get-ChildItem @$($PSBoundParameters - 'Force') # Splat all parameters but 'Force'
210
235
Get-ChildItem @$($PSBoundParameters - 'Force','WhatIf') # Splat all parameters but 'Force' and 'WhatIf'
211
- Get-ChildItem @$($PSBOundParameters + 'LiteralPath','Path') # Only splat 'LiteralPath' and 'Path'
236
+ Get-ChildItem @$($PSBoundParameters + 'LiteralPath','Path') # Only splat 'LiteralPath' and 'Path'
212
237
```
213
238
214
239
Today, PowerShell supports "adding" two hashtables with the '+' operator,
@@ -226,26 +251,3 @@ When using '-', the result will exclude all keys from the right hand side.
226
251
227
252
In either case,
228
253
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
-
230
- ## Alternate Proposals and Considerations
231
-
232
- ### Slicing operators
233
-
234
- The suggested use of '+' and '-' is perhaps surprising
235
- even though they correspond to Add and Remove, respectively.
236
- The actual operation is also similar to a union or intersection,
237
- so other operators should be considered, perhaps bitwise operators
238
- like '-bnot' and '-bor', or maybe new general purpose set operators.
239
-
240
- ### Postfix operator
241
-
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.
245
-
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.
0 commit comments