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

Brent Royal-Gordon brent at architechies.com
Mon May 30 08:01:30 CDT 2016


> // Allowed today:
> func takesATuple(tuple: (Int, Int)) {
>   let valueA = tuple.0
>   let valueB = tuple.1
>   // ...
> }
> 
> // Proposed syntax:
> func takesATuple(tuple (valueA, valueB): (Int, Int)) {
>   // use valueA
>   // use valueB
> }

Personally, I find this example confusing because the label is "tuple", which kind of reads like a keyword, and because you're using the same name for the label and variable. If I understand the semantics you're proposing correctly, I think it would be clearer to write this example like:

// Allowed today:
func takes(a tuple: (Int, Int)) {
  let valueA = tuple.0
  let valueB = tuple.1
  // ...
}

// Proposed syntax:
func takes(a (valueA, valueB): (Int, Int)) {
  // use valueA
  // use valueB
}

Incidentally, it may also be a good idea to define what happens if you write:

func takes((valueA, valueB): (Int, Int))

Normally, if there's no separate label and variable name, they're the same, but you can't have a label like `(valueA, valueB)`. I see two reasonably sensible answers here:

1. It's equivalent to writing `_ (valueA, valueB)`.
2. It's illegal. You have to write a label, or `_` if you don't want one.

My preference would be for #2, but you're the designer, not me.

-- 
Brent Royal-Gordon
Architechies



More information about the swift-evolution mailing list