I'd like to write an extension for tuples of (e.g.) two value in Swift. For instance, I'd like to write this swap
method:
let t = (1, "one")
let s = t.swap
such that s
would be of type (String, Int)
with value ("one", 1)
. (I know I can very easily implement a swap(t)
function instead, but that's not what I'm interested in.)
Can I do this? I cannot seem to write the proper type name in the extension
declaration.
Additionally, and I suppose the answer is the same, can I make a 2-tuple adopt a given protocol?
typealias
for it. For anything more complex you should usestruct
s or define global functions (maybe with generics) – Neukam