Created
March 31, 2016 00:19
-
-
Save ryantxr/8543a1fc7f00f2ced0ada6902097311a to your computer and use it in GitHub Desktop.
Example of infix operator overload in Swift
This file contains 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
struct Vector | |
{ | |
var x: Double = 0 | |
var y: Double = 0 | |
init(x: Double, y: Double) | |
{ | |
self.x = x | |
self.y = y | |
} | |
} | |
// v0 = v1 + v2 | |
func +(left: Vector, right: Vector) -> Vector | |
{ | |
return Vector(left.x + right.x, left.y + right.y) | |
} | |
// v1 += v2 | |
func += (inout left : Vector, right: Vector) | |
{ | |
left.x += right.x | |
left.y += right.y | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment