Created
June 17, 2016 14:01
-
-
Save smic/816d0674aaf3b20a22f49648fafca21c to your computer and use it in GitHub Desktop.
Operator conflict if using the * operator for CGPoints
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Cocoa | |
extension CGPoint { | |
public func length() -> CGFloat { | |
return hypot(self.x, self.y) | |
} | |
} | |
infix operator ⋅ { associativity left precedence 140 } | |
public func ⋅ (lhs: CGPoint, rhs: CGPoint) -> CGFloat { | |
return lhs.x * rhs.x + lhs.y * rhs.y | |
} | |
public func * (lhs: CGPoint, rhs: CGPoint) -> CGFloat { | |
return lhs.x * rhs.x + lhs.y * rhs.y | |
} | |
public func / (lhs: CGPoint, rhs: CGFloat) -> CGPoint { | |
return CGPoint(x:lhs.x / rhs, y:lhs.y / rhs) | |
} | |
let d = CGPoint(x: 1.0, y: 1.5) | |
let d2 = CGPoint(x: 2.0, y: 1.5) | |
var fraction = (d ⋅ d2) / d.length() / d2.length() | |
var fraction = (d * d2) / d.length() / d2.length() // <-- gives an error |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment