I'm trying to learn about how to do something simple with the generics facility in Swift. I've boiled one problem that I was facing down to a simple puzzle. Imagine that I want to write a value type Pair<A,B>
that has a flip
method on it that returns a copy of the pair, but with the two values (and their types) reversed.
struct Pair<A, B> {
let a: A
let b: B
init(first: A, second:B) {
a = first;
b = second;
}
func flip() -> Pair<B,A> {
return Pair(self.b,self.a)
}
}
When I write this I get an error on the line where I'm trying to return the new flipped pair. Cannot convert the expression's type 'Pair<A,B>' to type (first: A, second: B)'
Is what I'm trying to do even possible?