James Froggatt james.froggatt at me.com
Fri Jul 1 16:24:18 CDT 2016

```Thanks. I agree, a standard library operator would clearly be the preferred way to do this, at least for general use. Basing it upon the ‘as’ cast just means we don't have to have to deal with another magical operator (looking at the inability to overload ‘!’ and ‘?’), as well as easing the transition from Swift 2.

From James F

> On 1 Jul 2016, at 22:11, Vladimir.S <svabox at gmail.com> wrote:
>
>> On 01.07.2016 23:55, James Froggatt wrote:
>> Thanks for the response. I understand function overloading is possible, but it isn't nearly as convinient.
>
>
> Yes, agree.
>
>>
>> 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:
>>
>
>
> Personally I also think that we need such `as` conversions from one to another. At least because IMO there is a lot of code that already use the feature of tuple splatting.
>
>>
>> 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
>>
>
>
> Actually I also see a solution in having both: explicit 'as' conversion and in special operator. I feel like such special operator could be important for functional programming in Swift. 'as' conversion is very verbose and you have to write it for each function again and again(or use typealias), but operator will use one compiler magic for all functions.
>
> I believe core team should decide if they want to keep/allow such 'as' conversion in case the proposal will be accepted.
>
>>
>> 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.
>>
>
>
> :-) Yes, I also was surprised with just warnings in these places, but not errors. Don't know if it is a bug or not. Can someone explain ?
>
> Hint: check compilation warnings for your code.
>
>
>>> 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
>>>
>>> defaultDestination() => tupleize(moveTo2)
>>>
>>>
>>> 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
>>>>
>>>> //code without functional chaining
>>>>
>>>> let point = defaultDestination()
>>>> moveTo(x: point.x, y: point.y)
>>>>
>>>> } else {
>>>> }
>>>>
>>>> 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
>>>>
>>
```