Until we can extend non-nominal types we can expose this in a form of functions:
func reversed<T>(_ a: (T, T)) -> (T, T) {
(a.1, a.0)
}
func reversed<T>(_ a: (T, T, T)) -> (T, T, T) {
(a.2, a.1, a.0)
}
// ... up to a reasonable number of arguments
// usage:
reversed((1, 2)) // (2, 1)
Whether to include this in a standard library I don't know. Show your use case?
Unless you created another wrapper to access tuple values by index,
func value<T>(_ a: (T, T), at index: Int) -> T {
switch index {
case 0: return a.0
case 1: return a.1
default: fatalError("index out of bounds")
}
}
func setValue<T>(_ a: inout (T, T), _ value: T, at index: Int) {
switch index {
case 0: a.0 = value
case 1: a.1 = value
default: fatalError("index out of bounds")
}
}
tuple indexes are compile time constants, which raises a question why prefer "let b = reversed(a); (b.0, b.1)" form over a mere "(a.1, a.0)"