File tree Expand file tree Collapse file tree 1 file changed +34
-0
lines changed Expand file tree Collapse file tree 1 file changed +34
-0
lines changed Original file line number Diff line number Diff line change
1
+ # 凹语言支持运算符重载
2
+
3
+ - 时间:2024-04-13
4
+ - 撰稿:凹语言 开发组
5
+ - 转载请注明原文链接:[ https://wa-lang.org/smalltalk/st0040.html ] ( https://wa-lang.org/smalltalk/st0040.html )
6
+
7
+ ---
8
+
9
+ 在新发布的 [ v0.11.0] ( https://gitee.com/wa-lang/wa/releases/tag/v0.11.0 ) 版本中实验性地引入了运算符重载的特性。比如[ ` apple ` ] ( https://gitee.com/wa-lang/wa/tree/master/waroot/src/apple ) 标准库有以下代码:
10
+
11
+ ``` wa
12
+ #wa:operator + MyInt_add
13
+ type MyInt :struct {
14
+ V: int
15
+ }
16
+
17
+ func MyInt_add(x, y: MyInt) => int {
18
+ return x.V + y.V
19
+ }
20
+ ```
21
+
22
+ 其中` MyInt ` 类型针对` + ` 运算符进行重载,映射到了` MyInt_add ` 全局函数。可以像下面代码这样使用:
23
+
24
+ ``` wa
25
+ import "apple"
26
+
27
+ func main {
28
+ x1 := apple.MyInt{V: 100}
29
+ x2 := apple.MyInt{V: 42}
30
+ println(x1 + x2)
31
+ }
32
+ ```
33
+
34
+ 编译器会在编译时根据参数的类型选择合适的函数调用。目前这是一个实验性的特性,未来开发组将根据真实开发场景做调整和完善,也欢迎社区同学参与讨论。
You can’t perform that action at this time.
0 commit comments