D
D
Dima Sokolov2017-08-09 15:23:34
Swift
Dima Sokolov, 2017-08-09 15:23:34

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

Error:
Swift Ver. 3.1.1 (Release)
Platform: Linux (x86_64)
ERROR at line 19, col 21: extraneous argument labels 'x:y:z:' in call
return Point(x: left.x + right.x, y: left. y + right.y, z: left.z + right.z)
^~~~ ~~~ ~~~

Answer the question

In order to leave comments, you need to log in

1 answer(s)
I
iMaximus, 2017-08-09
@dimka11

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 question

Ask a Question

731 491 924 answers to any question