The following code does not compile (Xcode 10.1 default toolchain and Swift 5 Snapshot 2018-12-04 toolchain).
let counter = 119 let final: (min: Int, sec: Int) = counter.quotientAndRemainder(dividingBy: 60)
error: cannot convert value of type '(quotient: Int, remainder: Int)' to specified type '(min: Int, sec: Int)'
However, this rather clumsy workaround does exactly this:
let counter = 119 let foo = counter.quotientAndRemainder(dividingBy: 60) let bar: (Int, Int) = foo let final: (min: Int, sec: Int) = bar print(final) // (min: 1, sec: 59)
Couldn't/ shouldn't the type annotated first version work? This is not tuple shuffle, currently in discussion, but perhaps somehow related. Don't know…
Just had a case in my code where this could have made it better understandable at first glance.
Just getting my head around the tuple shuffle topic.
var a = (a: 1, b: 2) print(a) // (a: 1, b: 2) var b: (b: Int, a: Int) = a print(b) // (b: 2, a: 1)
This shuffles around the elements of the tuple according to their matching names. I would propose that the order of the elements always stays the same, but the names can freely be changed as long as the types match in the original order.