Operator Overloading
Inspect one language lane at a time so line-level text and code deltas stay readable.
Diff Lane
English
0 modified sections0 code block delta0 anchor delta
Diff Lane
中文
1 modified sections0 code block delta0 anchor delta
modified可以被重载的操作符textcode+5 lines, -24 lines
v1.0.5
Section Text
1
下表列出了所有可以被重载的操作符(优先级从高到低):2
3
| Operator | Description |4
|:--------------------|:----------------------|5
| `()` | Function call |6
| `[]` | Indexing |7
| `!` | NOT |8
| `-` | Negative |9
| `**` | Power |10
| `*` | Multiply |11
| `/` | Divide |12
| `%` | Remainder |13
| `+` | Add |14
| `-` | Subtract |15
| `<<` | Bitwise left shift |16
| `>>` | Bitwise right shift |17
| `<` | Less than |18
| `<=` | Less than or equal |19
| `>` | Greater than |20
| `>=` | Greater than or equal |21
| `==` | Equal |22
| `!=` | Not equal |23
| `&` | Bitwise AND |24
| `^` | Bitwise XOR |25
| <code>|</code> | Bitwise OR |26
27
需要注意的是:28
29
> **注意:**30
>31
> - 一旦在某个类型上重载了除关系操作符(`<`、`<=`、`>`、`>=`、`==` 和 `!=`)之外的其他二元操作符,并且操作符函数的返回类型与左操作数的类型一致或是其子类型,那么此类型支持对应的复合赋值操作符。当操作符函数的返回类型与左操作数的类型不一致且不是其子类型时,在使用对应的复合赋值符号时将报类型不匹配错误。32
> <!-- compile.error -->33
>34
> ```cangjie35
> open class MyClass {36
> var x: Int64 = 037
> public init (a: Int64) {38
> x = a39
> }40
>41
> public operator func +(right: MyClass): Int64 { // The above rules are not met42
> this.x + right.x43
> }44
> }45
>46
> main() {47
> var a = MyClass(5)48
> var b = MyClass(3)49
> a += b; // Error, type incompatible in this compound assignment expression50
> }51
> ```52
>53
> - 仓颉编程语言不支持自定义操作符,即不允许定义除上表中所列 `operator` 之外的其他操作符函数。54
> - 对于类型 `T`, 如果 `T` 已经默认支持了上述若干可重载操作符,那么通过扩展的方式再次为其实现同签名的操作符函数时将报重定义错误。例如,为数值类型重载其已支持的同签名算术操作符、位操作符或关系操作符等操作符时,为 `Rune` 重载同签名的关系操作符时,为 `Bool` 类型重载同签名的逻辑操作符、判等或不等操作符时,等等这些情况,均会报重定义错误。55
> <!-- compile.error -->56
>57
> ```cangjie58
> extend Int64 {59
> public operator func +(x: Int64, y: Int64): Int64 { // Error, invalid number of parameters for operator '+'60
> x + y61
> }62
> }63
> ```Code 1 · cangjie
1
Code 2 · cangjie
1
v1.1.0
Section Text
1
下表列出了所有可以被重载的操作符(优先级从高到低):2
3
| Operator | Description |4
|:--------------------|:----------------------|5
| `()` | Function call |6
| `[]` | Indexing |7
| `!` | NOT |8
| `-` | Negative |9
| `**` | Power |10
| `*` | Multiply |11
| `/` | Divide |12
| `%` | Remainder |13
| `+` | Add |14
| `-` | Subtract |15
| `<<` | Bitwise left shift |16
| `>>` | Bitwise right shift |17
| `<` | Less than |18
| `<=` | Less than or equal |19
| `>` | Greater than |20
| `>=` | Greater than or equal |21
| `==` | Equal |22
| `!=` | Not equal |23
| `&` | Bitwise AND |24
| `^` | Bitwise XOR |25
| <code>|</code> | Bitwise OR |26
27
**注意事项:**28
29
- 一旦在某个类型上重载了除关系操作符(`<`、`<=`、`>`、`>=`、`==` 和 `!=`)之外的其他二元操作符,并且操作符函数的返回类型与左操作数的类型一致或是其子类型,那么此类型支持对应的复合赋值操作符。当操作符函数的返回类型与左操作数的类型不一致且不是其子类型时,在使用对应的复合赋值符号时将报类型不匹配错误。30
31
<!-- compile.error -->32
33
34
- 仓颉编程语言不支持自定义操作符,即不允许定义除上表中所列 `operator` 之外的其他操作符函数。35
- 对于类型 `T`,如果 `T` 已默认支持上述某些可重载操作符,那么通过扩展方式再次为其实现相同签名的操作符函数将报重定义错误。例如:为数值类型重载其已支持的同签名算术操作符、位操作符或关系操作符,为 `Rune` 重载同签名的关系操作符,或为 `Bool` 类型重载同签名的逻辑操作符、判等或不等操作符,均会报重定义错误。36
37
<!-- compile.error -->Code 1 · cangjie
1
open class MyClass {2
var x: Int64 = 03
public init (a: Int64) {4
x = a5
}6
7
public operator func +(right: MyClass): Int64 { // The above rules are not met8
this.x + right.x9
}10
}11
12
main() {13
var a = MyClass(5)14
var b = MyClass(3)15
a += b; // Error, type incompatible in this compound assignment expression16
}Code 2 · cangjie
1
extend Int64 {2
public operator func +(x: Int64, y: Int64): Int64 { // Error, invalid number of parameters for operator '+'3
x + y4
}5
}