[swift-users] Tuple Member Extraction in Closure Parameters

Dennis Weissmann dennis at dennisweissmann.me
Thu May 5 09:12:21 CDT 2016


Hi swift-users,

Is it possible to “extract” the `tuple` tuple in the following code directly into two `Int` variables (`tuple` is of type `(Int, Int)`)?

let a = [0,1,2,3,4,5,6,7,8,9]
let b = [0,1,2,3,4,5,6,7,8,9]

let c = zip(a,b).reduce(0) { acc, tuple in
  acc + tuple.0 + tuple.1
}

Like so (does not compile):

let d = zip(a,b).reduce(0) { acc, (value1, value2) in
  acc + value1 + value2
}

My problem with the current approach is that is makes the calculation very unreadable / not comprehensible (i.e. `.0` and `.1` - my actual calculation and parameters are a bit more complicated). A workaround is to declare two local variables:

let e = zip(a,b).reduce(0) { acc, tuple in
  let value1 = tuple.0
  let value2 = tuple.1
  return acc + value1 + value2
}

Is this intentionally not supported?

Thanks,

- Dennis

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-users/attachments/20160505/943221df/attachment.html>


More information about the swift-users mailing list