Simplifying Function Parameter List (when calling)


(Ted van Gaalen) #1

(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.

Some examples:
  
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:

    fooWithThing( thing
                           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[3] )

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” )
or so:

     fooWithVariadic( a: 12.34 …(72 37 aNumber 65 * 56) c: “Saturn” ) // … has 4 values here.

Kind Regards
TedvG


(David Sweeris) #2

Commas may not be necessary from the parser’s point of view, but to me having a non-whitespace delimiter significantly aids readability.

- Dave Sweeris

···

On Mar 14, 2016, at 4:34 PM, Ted F.A. van Gaalen via swift-evolution <swift-evolution@swift.org> wrote:

(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.