11---
22RFC : 0002
33Author : Jason Shirk
4- Status : Draft
4+ Status : Draft-Accepted
55Area : Splatting
66Comments Due : 3/31/2016
7+ Edits : Joey Aiello
78---
89
910# Generalized Splatting
@@ -47,7 +48,7 @@ $addTypeParams = @{
4748 MemberType = 'NoteProperty'
4849 Value = 42
4950}
50- Update-TypeData @addTypeParams
51+ Update-TypeData @addTypeParams
5152```
5253
5354This works, but feels a bit messy because of the need for a variable,
@@ -60,7 +61,7 @@ $PSBoundParameters.Remove('SomeExtraParam')
6061Command @PSBoundParameters
6162```
6263
63- This proposal suggesst a syntax that improves this scenario as well.
64+ This proposal suggests a syntax that improves this scenario as well.
6465
6566## Specification
6667
@@ -118,7 +119,7 @@ Get-ChildItem @$myArgs
118119```
119120
120121The 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:
122123
123124``` PowerShell
124125$myArgs = @{ Path = $pwd; ExtraStuff = 1234 }
@@ -130,6 +131,15 @@ If '@' is the splatting operator,
130131adding the '?' is suggestive of being more permissive,
131132much like the C# '?.' member access operator.
132133
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+
133143### Splatting in method invocations
134144
135145Today, named arguments are only supported when calling commands,
@@ -160,7 +170,7 @@ and via splatting in the same invocation:
160170# Must be an error, parse time or runtime, because startIndex
161171# is specified positionally and via splatting.
162172$subStringArgs = @{startIndex = 2}
163- $str.SubString(2 , @$subStringArgs)
173+ $str.SubString(3 , @$subStringArgs)
164174```
165175
166176Multiple splatted arguments are not allowed:
@@ -177,29 +187,58 @@ The splatted argument must be last.
177187$str.SubString(@@{length=2}, 2)
178188```
179189
180- ### Splatting in switch cases
190+ ## Alternate Proposals and Considerations
181191
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 ` :
184196
185197``` 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)
190202```
191203
192- With splatting, this can be simplified:
204+ However, some situations may make it ambiguous or unclear as to which overload you're invoking.
193205
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 );
198215}
199216```
200217
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+
201237### Modifying hashtables for splatting
202238
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+
203242Sometimes it is useful to provide a 'slice' of a hashtable,
204243e.g. you want to remove or include specific keys.
205244The 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
208247``` PowerShell
209248Get-ChildItem @$($PSBoundParameters - 'Force') # Splat all parameters but 'Force'
210249Get-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'
212251```
213252
214253Today, 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.
227266In either case,
228267it is not an error to specify a key in the right hand side operand that is not present in the left hand side.
229268
230- ## Alternate Proposals and Considerations
231-
232- ### Slicing operators
233-
234269The suggested use of '+' and '-' is perhaps surprising
235270even though they correspond to Add and Remove, respectively.
236271The actual operation is also similar to a union or intersection,
237272so 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.
239274
240- ### Postfix operator
275+ ---------------
241276
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
245278
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