[swift-evolution] Variadics through tuples

James Froggatt james.froggatt at me.com
Fri Jul 1 15:55:17 CDT 2016


Thanks for the response. I understand function overloading is possible, but it isn't nearly as convinient.

However, if explicit conversion is the way forward, then in the spirit of keeping backwards-compatibility in some form, we could simply allow this:

let paramsClosure: (Int, Int) -> () = …
let tupleClosure = paramsClosure as ((Int, Int)) -> ()


The ‘as’ operator is already used for various type system magic (like converting T to Optional<T>), so this seems fairly natural. There shouldn't be any ambiguity, since overloads taking tuples would have either different labels or type signature:


moveTo(x: Int, y: Int)
moveTo(_: (x: Int, y: Int)) //sneaky
moveTo(x: (x: Int, y: Int), y: Int) //plain dumb

let a = moveTo(x:y:)
let b = moveTo(_:)
let c = moveTo(x:y:) as ((x: Int, y: Int), Int) -> () //cast needed, same as for any type-based overload

let d = moveTo(x:y:) as ((Int, Int)) -> () //unambiguously the first function

Since this is clearly an explicit cast, which doesn't fall under the ‘remove implicit tuple splat’ proposal, can we explicity decide to keep this?


A splat operator would then be user-definable (meaning fewer magic operators), just being applied to the function rather than the arguments:

moveTo(x: point.x, y: point.y)

(moveTo*)(point)

So, rather than a language addition, or even a modification of an existing proposal, this is something which sensibly should still be allowed even once ‘implicit tuple splat’ is removed. We'd actually be losing a decent feature if we remove this completely, as is acknowledged by the suggestion of an explicit splat operator in the proposal.

From James F


PS: Actually, I'm surprised your code compiles, since I removed the ‘rethrows’ decoration from the operator's function signature for brevity, but left the ‘try’ in the implementation itself. Does Swift promote non-throwing functions to throwing ones like that? I have to double-check I don't have stray ‘try’s lying around.

> On 1 Jul 2016, at 19:15, Vladimir.S <svabox at gmail.com> wrote:
> 
> Yes, I expect that with the implemented proposal SE-0110 your code will not compile because `moveTo` has type `(Int, Int)->Void` and not required `((Int, Int))->Void`. You'll need to change the moveTo to accept tuple argument or use some function/operator to transform argument list parametrized function to function with tuple argument.
> 
> For example (this compiles now and IMO should after SE-0110 implemented, if accepted):
> 
> //function application operator
> infix operator => {}
> func =><In, Out>(a: In, b: (In) -> Out) -> Out {
>    return try b(a)
> }
> 
> infix operator ?=> {}
> func ?=><In, Out>(a: In?, b: (In) -> Out?) -> Out? {
>    if let unwrapped = a {
>        return try b(unwrapped)
>    }
>    return nil
> }
> 
> //some defined functions
> 
> func defaultDestination() -> (x: Int, y: Int) {return (1,1) }
> func loadedDestination() -> (x: Int, y: Int)? {return nil }
> 
> 
> // ---- Variant 1 ----
> func moveTo1(point: (x: Int, y: Int)) {print("move1 to: ", point.x, point.y)}
> 
> 
> // ---- Variant 2 ----
> func moveTo2(x: Int, y: Int) {print("move2 to: ", x, y)}
> 
> func tupleize<T,U,V>(_ f: (T,U)->V ) -> ( ((T, U))->V ) {
>    return { tu in return f(tu.0, tu.1) }
> }
> 
> 
> //actual code
> 
> defaultDestination() => moveTo1
> (loadedDestination() ?=> moveTo1) ?? print("load1 failed")
> 
> defaultDestination() => tupleize(moveTo2)
> (loadedDestination() ?=> tupleize(moveTo2)) ?? print("load2 failed")
> 
> 
> If we'll have such `tupleize` as built-in operator, then things will be even better. Don't see any problem here just like you, for example, don't expect function of (Int, (Int, String))->() will be accepted where (Int, Int, String)->() is required an so on. List of function arguments is not tuple at these days and so IMO only explicit conversion can exist to accept second when first is required and vice-versa. But I'm storng +1 to have such handy convertion operator.
> 
> 
>> On 01.07.2016 17:16, James Froggatt via swift-evolution wrote:
>> Currently, the following code is allowed:
>> 
>> //function application operator
>> infix operator => {}
>> func =><In, Out>(a: In, b: In -> Out) -> Out {
>>    return try b(a)
>> }
>> 
>> infix operator ?=> {}
>> func ?=><In, Out>(a: In?, b: In -> Out?) -> Out? {
>>    if let unwrapped = a {
>>        return try b(a)
>>    }
>>    return nil
>> }
>> 
>> //some defined functions
>> 
>> func defaultDestination() -> (x: Int, y: Int)
>> func loadedDestination() -> (x: Int, y: Int)?
>> func moveTo(x: Int, y: Int)
>> 
>> //actual code
>> 
>> defaultDestination() => moveTo
>> loadedDestination() ?=> moveTo ?? print("load failed")
>> 
>> //code without functional chaining
>> 
>> let point = defaultDestination()
>> moveTo(x: point.x, y: point.y)
>> 
>> if let loaded = loadedDestination() {
>>    moveTo(x: loaded.x, y: loaded.y)
>> } else {
>>    print("load failed")
>> }
>> 
>> I'm expecting this to stop working at some point in Swift 3's development, since it is related to tuple splat. I've heard talk of tuple splat returning in the future through an operator, with the stand-in syntax:
>> 
>> moveTo(*defaultDestination())
>> 
>> So, how would a functional chaining operator work under these conditions? It would require a second variadic splat operator:
>> 
>> defaultDestination() => *moveTo(x:y:)
>> 
>> The motivation for removing the standard form of tuple splat is that the calling syntax looks like an overload. But in this case, an explicit splat operator doesn't add any clarity, since the function being referred to can be unambiguous.
>> 
>> 
>> 
>> So, my question is whether this is worth removing full support for in the first place. This behaviour can be make to fit Swift 3's distinction of parameter lists and tuples, by applying specific rules to the existing behaviour to create a lightweight variadics system:
>> 
>> takesAClosure<T>(_: (T) -> ()) //closure explicitly takes a single parameter
>> 
>> takesAClosure<T>(_: T -> ()) //closure takes any number of parameters
>> 
>> takesAClosure<T>(_: T, _: T -> ()) //closure takes any number of parameters, but must have a parameter list which can be directly represented as a tuple
>> 
>> 
>> 
>> Either way, I'm hoping whatever syntax ends up chosen for variadics is nearly as simple to use as tuple splat has been, tuples are (literally) made for this purpose. I'll be disappointed to see this feature removed.
>> _______________________________________________
>> swift-evolution mailing list
>> swift-evolution at swift.org
>> https://lists.swift.org/mailman/listinfo/swift-evolution
>> 


More information about the swift-evolution mailing list