柯里化
func add(num1: Int)(num2: Int) -> Int {
return num1 + num2
}
let addThree = add(3)
let result = addThree(num2: 8)
操作符
- 可以直接重载+ - 乘操作符,在swift中有了这些操作符的定义,如果想自定义操作符,比如点乘+*,需要先声名:
infix operator +* {
associativity none
precedence 160
}
infix
- 为中位操作符,前后都是输入,还有prefix和postfix;
associativity
- 结合律,加法和减法的为left,点乘不和其它乘结合,这里为none;
precedence
- 优先级,swift中,乘除法为150, 加减法为140,这里点乘定义为160.
运算:
struct Point {
var x = 0.0
var y = 0.0
}
func +*(left: Point, right: Point) -> Double {
return left.x * right.x + left.y * right.y
}
let p1 = Point(x: 3.0, y: 5.0)
let p2 = Point(x: 4.0, y: 2.0)
let result2 = p1 +* p2