Repository navigation
Expand file tree
/
Copy path03_annotations.candle
More file actions
249 lines (199 loc) · 5.66 KB
/
Copy path03_annotations.candle
File metadata and controls
249 lines (199 loc) · 5.66 KB
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
/// Candle 语言 @注解模板
/// 展示所有内置注解的使用方法
// ==================== @builtin 注解 ====================
// 用于标记由运行时实现的内置函数/方法
// 编译器会将这些调用直接链接到 Rust runtime
class ArrayFloat inherit Object {
/// @builtin 标记静态方法由运行时实现
@builtin
static ArrayFloat _allocate(List<int> shape, int dtype);
/// @builtin 标记实例方法由运行时实现
@builtin
double _get_flat(int index);
@builtin
void _set_flat(int index, double value);
@builtin
double _raw_sum();
}
// ==================== @operator 注解 ====================
// 用于运算符重载,定义类的运算符行为
class Vector inherit Object {
double x;
double y;
Vector(double x, double y) {
this.x = x;
this.y = y;
}
/// 重载加法运算符 +
@operator
Vector __add__(Vector other) {
return Vector(this.x + other.x, this.y + other.y);
}
/// 重载减法运算符 -
@operator
Vector __sub__(Vector other) {
return Vector(this.x - other.x, this.y - other.y);
}
/// 重载乘法运算符 *
@operator
Vector __mul__(double scalar) {
return Vector(this.x * scalar, this.y * scalar);
}
/// 重载除法运算符 /
@operator
Vector __div__(double scalar) {
return Vector(this.x / scalar, this.y / scalar);
}
/// 重载等于运算符 ==
@operator
bool __eq__(Vector other) {
return this.x == other.x && this.y == other.y;
}
/// 重载不等于运算符 !=
@operator
bool __ne__(Vector other) {
return this.x != other.x || this.y != other.y;
}
/// 重载索引访问运算符 []
@operator
double __get__(int index) {
if (index == 0) {
return this.x;
}
return this.y;
}
/// 重载索引赋值运算符 []=
@operator
void __set__(int index, double value) {
if (index == 0) {
this.x = value;
} else {
this.y = value;
}
}
}
// ==================== @deprecated 注解 ====================
// 标记已废弃的函数/类,编译器会发出警告
/// @deprecated 标记废弃的函数
@deprecated
void oldFunction() {
print("这个函数已废弃,请使用 newFunction\n");
}
/// 替代函数
void newFunction() {
print("这是新函数\n");
}
/// @deprecated 标记废弃的类
@deprecated
class OldClass {
void doSomething() {
print("旧类实现\n");
}
}
// ==================== @override 注解 ====================
// 标记重写父类方法(可选,但推荐用于代码清晰)
class Animal inherit Object {
void speak() {
print("Animal speaks\n");
}
}
class Dog inherit Animal {
/// @override 明确标记这是重写方法
@override
void speak() {
print("Woof!\n");
}
}
// ==================== @async 注解 ====================
// 标记异步函数(如果 Candle 支持异步编程)
// @async
// Future<String> fetchData(String url) {
// // 异步操作
// return Future.delayed("data");
// }
// ==================== @parallel 注解 ====================
// 标记可并行执行的代码(如果 Candle 支持并行编程)
// @parallel
// void parallelTask() {
// // 并行执行的代码
// }
// ==================== @inline 注解 ====================
// 建议编译器内联函数(优化提示)
// @inline
// int fastAdd(int a, int b) {
// return a + b;
// }
// ==================== @pure 注解 ====================
// 标记纯函数(无副作用,相同输入总是产生相同输出)
// @pure
// int multiply(int a, int b) {
// return a * b;
// }
// ==================== @test 注解 ====================
// 标记测试函数(测试框架使用)
// @test
// void testAddition() {
// assert(add(2, 3) == 5);
// }
// ==================== 综合示例 ====================
int main() {
print("=== Candle 注解演示 ===\n");
// 测试 @builtin
List<int> shape = List<int>();
shape.add(3);
ArrayFloat arr = ArrayFloat._allocate(shape, 2);
arr._set_flat(0, 1.0);
arr._set_flat(1, 2.0);
arr._set_flat(2, 3.0);
print("Array sum: ");
print(arr._raw_sum());
print("\n");
// 测试 @operator
Vector v1 = Vector(1.0, 2.0);
Vector v2 = Vector(3.0, 4.0);
Vector v3 = v1 + v2; // 调用 __add__
print("Vector addition: (");
print(v3.x);
print(", ");
print(v3.y);
print(")\n");
// 测试索引运算符重载
print("v1[0] = ");
print(v1[0]);
print("\n");
v1[1] = 5.0; // 调用 __set__
print("v1[1] = ");
print(v1[1]);
print("\n");
// 测试 @deprecated(会产生编译警告)
// oldFunction(); // 取消注释会看到警告
// 测试 @override
Dog dog = Dog();
dog.speak();
print("\n=== 注解演示完成 ===\n");
return 0;
}
// ==================== 注解总结 ====================
/*
Candle 语言主要注解:
1. @builtin - 标记运行时实现的函数
- 用于核心库和性能关键代码
- 编译器直接链接到 Rust runtime
2. @operator - 运算符重载
- __add__, __sub__, __mul__, __div__ (算术)
- __eq__, __ne__, __lt__, __gt__ (比较)
- __get__, __set__ (索引)
- __call__ (可调用对象)
3. @deprecated - 标记废弃代码
- 编译器发出警告
- 用于平滑过渡 API 变更
4. @override - 标记重写方法
- 提高代码可读性
- 帮助捕获签名错误
其他可能支持的注解:
- @async - 异步函数
- @parallel - 并行执行
- @inline - 内联优化
- @pure - 纯函数
- @test - 测试函数
*/