According to the Swift Programming Guide, operator overloading is allowed and actually quite versatile. However, I have been unable to get it working in the playground.
For example, the Equatable
protocol wants this: func ==(lhs:Self, rhs:Self) -> Bool
Let's say I make a simple Location3D
struct:
struct Location3D
{
var x : Double
var y : Double
var z : Double
}
Now I want this Location3D
to implement the Equatable
protocol, so I add it along with this method:
func ==(lhs: Self, rhs: Self) -> Bool
{
return lhs.x == rhs.x &&
lhs.y == rhs.y &&
lhs.z == rhs.z
}
I get the compiler error of operators are only allowed at global scope. Huh?
So I tried adding @infix
to the function, moving the function to an extension, changing the type to a class instead... all to no avail.
Am I missing something? How are you supposed to implement Equtable
and Comparable
when operators don't seem to work?
global scope
would point to a location outside of any Class or extension declaration – Jene