Since (AFAIK) the Tuple
type does not conform to a Protocol
(and does not even have a name) it's very hard to do what you need.
This is the closest I could get (maybe others can provide more elegant solutions).
Typealias
First of all lets define a couple of typealiases
typealias Tuple2 = (Any, Any)
typealias Tuple3 = (Any, Any, Any)
Yes, some readers now understand where I am going and probably don't like it...
I don't like it neither
SequenceType
Now let's extend the protocol SequenceType
adding the foo
method when the Element
of the sequence is Tuple2
...
extension SequenceType where Generator.Element == Tuple2 {
func foo() {}
}
or Tuple3
extension SequenceType where Generator.Element == Tuple3 {
func foo() {}
}
Array
Next lets define and populate an array of Tuple2
let list: [Tuple2] = [(1,2)]
Now the extension is applied and we can write
list.foo()
Disclaimer :D
This does work only if the array is explicitly declared as [Tuple2]
(or [Tuple3]
).
Something like this does not work
let list = [(1,2)]
list.foo() // compile error