@@ -4,6 +4,7 @@ Author: Jason Shirk
44Status : Draft
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,16 @@ 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)
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)
164183```
165184
166185Multiple splatted arguments are not allowed:
@@ -177,29 +196,35 @@ The splatted argument must be last.
177196$str.SubString(@@{length=2}, 2)
178197```
179198
180- ### Splatting in switch cases
181199
182- It can be awkward to match multiple conditions with a single switch statement.
183- For example:
200+ ## Alternate Proposals and Considerations
184201
185- ``` PowerShell
186- switch ($color) {
187- { $_ -eq 'Red' -or $_ -eq 'Blue' -or $_ -eq 'Green' } { $true }
188- default { $false }
189- }
190- ```
202+ ### Slicing operators
191203
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.
193209
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.
200222
201223### Modifying hashtables for splatting
202224
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+
203228Sometimes it is useful to provide a 'slice' of a hashtable,
204229e.g. you want to remove or include specific keys.
205230The 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
208233``` PowerShell
209234Get-ChildItem @$($PSBoundParameters - 'Force') # Splat all parameters but 'Force'
210235Get-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'
212237```
213238
214239Today, 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.
226251
227252In either case,
228253it 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