[swift-evolution] Simplifying Function Parameter List (when calling)
Ted F.A. van Gaalen
tedvgiosdev at gmail.com
Mon Mar 14 16:34:41 CDT 2016
(spawned from subject: "Make the first parameter in a function follow the same rules as the others”)
Idea: Eliminate commas from the parameter list when calling a function, because they are not necessary.
Function declaration as they ares now need not to be changed.
(including (as recently proposed by others) that the 1st parameter now has the same rules as the other parameters).
I’ve dropped the thought of an arbitrary parameter sequence (when calling.)
because this is only possible when all parameters are named,
which seems to be what many people don’t want.
Removing the commas as separators from the parameter list and instead
regarding space(s) and also linefeeds as separators has significant advantages:
- it is possible to use multiple variadic parameters
- there is no obligation to put a variadic sequence at the end of the parameter list.
- it looks cleaner and is more readable.
- easier and faster to type.
1. Calling a function with all parameters labeled:
foo( a: thing // The colon : always signifies that it is a label, not a var/value
b: 12,45, 65, 56, 456 // b and c are variadic. .
c: “aeroplane”, ”car”, ”rocket”
d: temperature )
// The commas embedded in variadic parameters are not interpreted as parameter separators!
2. Calling a function with the first two parameters unlabeled:
12, average(12,6,36), 45, 65, 56, 456 // this line is just one variadic parameter
c: “aeroplane”, ”car”, ”rocket” // yet another variadic parameter
d: temperature )
3. Calling a function where all labels are omitted (but not in the embedded function here)
setFlightVector( 10.0 34.5 ascend( horizon: 7.99 at: altitude ) plane: dc )
Note that spaces embedded in a variadic row do not cause problems
because the variadic items must be connected by embedded commas.
As far as I can see, this should be possible.
Unless there would be problems / conflicts with other Swift
language usage or components?
I hope that I am not missing something.
btw: The problem with variadic parameters would not be
there if instead of this a collection type e.g. array would be passed.
However, I understand that for calls to legacy functions, this cannot
be removed? I’d suggest this could be easily solved with a
keyword or special builtin function e.g. like so:
fooWithVariadic( a: 12.34
variadic(12 34 534 65 56)
c: “Moon” )
fooWithVariadic( a: 12.34 …(72 37 aNumber 65 * 56) c: “Saturn” ) // … has 4 values here.
More information about the swift-evolution