Swift学习21:如何在swift中自定义运算符

张建 lol

自定义运算符

  • 除了实现标准的运算符,在 swift 当中还可以声明和实现自定义运算符(custom operators)

  • 新的运算符要在全局作用域内,使用 operator 关键字进行声明,同时还要指定 prefix、infix 或者 postfix 限定符

【示例1】:一元运算符

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
struct Vector2D {
var x = 0.0,y = 0.0
}
// 声明一元运算符
prefix operator +++

// 扩展
extension Vector2D {
static prefix func +++ (vector: Vector2D) -> Vector2D {
return Vector2D(x: vector.x + vector.x,y: vector.y + vector.y)
}
}
let vectorToDouble = Vector2D(x: 1.0,y: 2.0)
let doubleVector = +++vectorToDouble
print("\(doubleVector.x) \(doubleVector.y)")

编译运行,查看打印结果

1
2.0 4.0

【示例2】:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
// 中缀运算符
struct Vector2D {
var x = 0.0,y = 0.0
}
infix operator +-: AdditionPrecedence
extension Vector2D {
static func +- (left: Vector2D,right: Vector2D) -> Vector2D {
return Vector2D(x: left.x + right.x,y: left.y - right.y)
}
}
let firstVector = Vector2D(x: 1.0,y: 3.0)
let secondVector = Vector2D(x: 2.0,y: 5.0)
let result = firstVector +- secondVector
print("\(result.x) \(result.y)")

编译运行,查看打印结果:

1
3.0 -2.0

【示例3】:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
// 中缀运算符
struct Vector2D {
var x = 0.0,y = 0.0
}
infix operator *^: MultiplicationPrecedence
extension Vector2D {
static func *^ (left: Vector2D,right: Vector2D) -> Vector2D {
return Vector2D(x: left.x * right.x,y: left.y * left.y + right.y * right.y)
}
}
let firstVector = Vector2D(x: 1.0,y: 3.0)
let secondVector = Vector2D(x: 2.0,y: 5.0)
let result = firstVector *^ secondVector
print("\(result.x) \(result.y)")

编译运行,查看打印结果:

1
2.0 34.0

自定义中缀运算符的优先级和结合性

  • 自定义 中缀(infix) 运算符也可以指定优先级和结核性

  • 每一个自定义的中缀运算符都属于一个 优先级组

  • 优先级组指定了自定义中缀运算符和其他中缀运算符的关系

【示例】:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
// 优先级和结合性
struct Vector2D {
var x = 0.0,y = 0.0
}
infix operator +-: AdditionPrecedence
extension Vector2D {
static func +- (left: Vector2D,right: Vector2D) -> Vector2D {
return Vector2D(x: left.x + right.x,y: left.y - right.y)
}
}
infix operator *^: MultiplicationPrecedence
extension Vector2D {
static func *^ (left: Vector2D,right: Vector2D) -> Vector2D {
return Vector2D(x: left.x * right.x,y: left.y * left.y + right.y * right.y)
}
}
let firstVector = Vector2D(x: 1.0,y: 2.0)
let secondVector = Vector2D(x: 2.0,y: 3.0)
let thirdVector = Vector2D(x: 3.0,y: 4.0)
let result = firstVector +- secondVector *^ thirdVector
print("\(result.x) \(result.y)")

编译运行,查看打印结果:

1
7.0 -23.0

【注】:先计算 *^ 优先级组,再计算 +- 优先级组

x: 2 * 3 = 6 + 1 = 7.0
y: 3 * 3 + 4 * 4 = 2 - 25 = -23

  • 接下来我们自己定义一个优先级组
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
// 自定义优先级和结合性
struct Vector2D {
var x = 0.0,y = 0.0
}
infix operator +-: AdditionPrecedence
extension Vector2D {
static func +- (left: Vector2D,right: Vector2D) -> Vector2D {
return Vector2D(x: left.x + right.x,y: left.y - right.y)
}
}
infix operator *^: MyPrecedencegroup

// 我的优先级组
precedencegroup MyPrecedencegroup {
// 结合性
associativity: left
// 优先级
lowerThan: AdditionPrecedence
}

extension Vector2D {
static func *^ (left: Vector2D,right: Vector2D) -> Vector2D {
return Vector2D(x: left.x * right.x,y: left.y * left.y + right.y * right.y)
}
}
let firstVector = Vector2D(x: 1.0,y: 2.0)
let secondVector = Vector2D(x: 2.0,y: 3.0)
let thirdVector = Vector2D(x: 3.0,y: 4.0)
let result = firstVector +- secondVector *^ thirdVector
print

编译运行,查看打印结果:

1
9.0 17.0

【注】:先计算 *^ 优先级组,再计算 +- 优先级组

x:1 + 2 = 3 * 3 = 9.0
y:2 - 3 = -1 * -1 + 4 * 4 = 17.0

  • Post title:Swift学习21:如何在swift中自定义运算符
  • Post author:张建
  • Create time:2023-02-23 04:03:04
  • Post link:https://redefine.ohevan.com/2023/02/23/Swift课程/Swift学习21:如何在swift中自定义运算符/
  • Copyright Notice:All articles in this blog are licensed under BY-NC-SA unless stating additionally.
On this page
Swift学习21:如何在swift中自定义运算符