Answer the question
In order to leave comments, you need to log in
Operator overloading in Swift?
struct Point{
var x = 0
var y = 0
var z = 0
init(_ x: Int, _ y: Int, _ z: Int){
self.x = x
self.y = y
self.z = z
}
}
let MyFirstPoint = Point(1,2,3)
let MySecondPoint = Point(2,3,4)
extension Point {
static func + (left: Point, right: Point) -> Point {
return Point(x: left.x + right.x, y: left.y + right.y, z: left.z + right.z)
}
}
var ThirdPoint = MyFirstPoint + MySecondPoint
Answer the question
In order to leave comments, you need to log in
Everything works, I fixed only one line, namely return Point(x: left.x + right.x, y: left.y + right.y, z: left.z + right.z)
Or you can leave this line alone, then so
In swift 3, All function parameters have the same outer parameter name as the inner one, unless otherwise specified.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question