[swift-evolution] [Pitch] Tuple Destructuring in Parameter Lists

Erica Sadun erica at ericasadun.com
Mon May 30 16:09:50 CDT 2016


> On May 30, 2016, at 2:39 PM, Chris Lattner via swift-evolution <swift-evolution at swift.org> wrote:
> 
>> On May 30, 2016, at 6:01 AM, Brent Royal-Gordon via swift-evolution <swift-evolution at swift.org> wrote:
>> 
>> // Proposed syntax:
>> func takes(a (valueA, valueB): (Int, Int)) {
>> // use valueA
>> // use valueB
>> }
> 
> FWIW, Swift 1 supported tuple destructuring in parameter lists, and we took it out to simplify the language and eliminate special cases.  Whereas as a very early version of swift modeled parameter lists using patterns (something very common in functional programming languages) we have progressively and intentionally move away from that approach.
> 
> -Chris

You can't splat but you can decompose a tuple by assignment:

let a = (1, 2, 3)
func foo(v1: Int, v2: Int, v3: Int) { print (v1, v2, v3) }

// Still works:
let (b, c, d) = a; print(b, c, d)

// And this works after the assignment:
foo(v1: b, v2: c, v3: d)

// No longer works:
foo(a) // tuple splat is gone

// These all work though:
func bar(arg: (Int, Int, Int)) { print(arg.0, arg.1, arg.2) }
bar(arg: a)
bar(arg: (b, c, d))

// You can add field names in the func's decl type
func blort(arg: (x: Int, y: Int, z: Int)) { print(arg.x, arg.y, arg.z) }
blort(arg: a) // works
blort(arg: (b, c, d)) // works

// But the following doesn't work, Error is "cannot 
// convert value of (l: Int, m: Int, n: Int)"
blort(arg: (l: b, m: c, n: d))

I vaguely remember a discussion onlist about creating typealiases and then using casting to convert between structurally identical tuples but I don't think it went anywhere or had a strong use case.

-- E

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20160530/ea9f1f10/attachment.html>


More information about the swift-evolution mailing list