Reputation: 1
I've been learning Swift and have a question about using Generics with Operator Overloading. This is my requirement:
It seems Swift isn't smart enough to allow me to write a generic operator overload function that references the generic array [T] without some workarounds?
I have read this post: [http://www.raywenderlich.com/80818/operator-overloading-in-swift-tutorial][1] and the solution given there seems surprisingly complicated.
I just wondered what the general consensus amongst the pro's here is? Sorry, I will post a code sample as an edit shortly.
Paul
Upvotes: 0
Views: 1577
Reputation: 997
Here's how you can do that. Its very easy, you just need to ensure that T is Equatable.
struct Matrix<T> {
// Definition goes here.
var array = [T]()
}
func ==<T: Equatable>(lhs: Matrix<T>, rhs: Matrix<T>) -> Bool {
return lhs.array == rhs.array
}
Upvotes: 2