Skip to content

Commit e09b14f

Browse files
authored
Merge pull request #154 from joeyaiello/RFC0002
Edits to RFC0002 after committee discussion
2 parents 1e8a5c6 + 7937915 commit e09b14f

File tree

1 file changed

+85
-33
lines changed

1 file changed

+85
-33
lines changed

1-Draft/RFC0002-Generalized-Splatting.md renamed to 2-Draft-Accepted/RFC0002-Generalized-Splatting.md

Lines changed: 85 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
---
22
RFC: 0002
33
Author: Jason Shirk
4-
Status: Draft
4+
Status: Draft-Accepted
55
Area: Splatting
66
Comments 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

5354
This works, but feels a bit messy because of the need for a variable,
@@ -60,7 +61,7 @@ $PSBoundParameters.Remove('SomeExtraParam')
6061
Command @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

120121
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:
122123

123124
```PowerShell
124125
$myArgs = @{ Path = $pwd; ExtraStuff = 1234 }
@@ -130,6 +131,15 @@ If '@' is the splatting operator,
130131
adding the '?' is suggestive of being more permissive,
131132
much 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

135145
Today, 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

166176
Multiple 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+
203242
Sometimes it is useful to provide a 'slice' of a hashtable,
204243
e.g. you want to remove or include specific keys.
205244
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
208247
```PowerShell
209248
Get-ChildItem @$($PSBoundParameters - 'Force') # Splat all parameters but 'Force'
210249
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'
212251
```
213252

214253
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.
227266
In either case,
228267
it 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-
234269
The suggested use of '+' and '-' is perhaps surprising
235270
even though they correspond to Add and Remove, respectively.
236271
The actual operation is also similar to a union or intersection,
237272
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.
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

Comments
 (0)