Skip to content
This repository was archived by the owner on Aug 18, 2025. It is now read-only.

Commit f847ef6

Browse files
committed
更新README.md,新增枚举类型的详细说明,包括基础枚举、带参数的枚举及复杂枚举示例,进一步完善文档以支持CodeNothing语言的枚举功能。
1 parent d2776fe commit f847ef6

File tree

1 file changed

+53
-1
lines changed

1 file changed

+53
-1
lines changed

README.md

Lines changed: 53 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -183,4 +183,56 @@ CodeNothing 支持通过动态库扩展功能。动态库必须遵循以下规
183183
1. 必须导出一个名为 `cn_init` 的函数,该函数返回一个包含库函数的 HashMap 指针。
184184
2. 库函数必须接受 `Vec<String>` 类型的参数,并返回 `String` 类型的结果。
185185

186-
详细信息请参阅 `library_example` 目录中的示例库和说明文档。
186+
详细信息请参阅 `library_example` 目录中的示例库和说明文档。
187+
188+
### 枚举类型 (Enum)
189+
190+
CodeNothing 支持类似 Rust 的枚举类型,可以定义带有或不带有参数的枚举变体。
191+
192+
#### 基础枚举
193+
194+
```
195+
enum Color {
196+
Red,
197+
Green,
198+
Blue
199+
};
200+
201+
// 使用枚举
202+
red : Color = Color::Red;
203+
green : Color = Color::Green;
204+
```
205+
206+
#### 带参数的枚举
207+
208+
```
209+
enum Shape {
210+
Circle(float),
211+
Rectangle(float, float),
212+
Triangle(float, float, float)
213+
};
214+
215+
// 创建带参数的枚举变体
216+
circle : Shape = Shape::Circle(5.0);
217+
rectangle : Shape = Shape::Rectangle(10.0, 20.0);
218+
triangle : Shape = Shape::Triangle(3.0, 4.0, 5.0);
219+
```
220+
221+
#### 复杂枚举示例
222+
223+
```
224+
enum Message {
225+
Quit,
226+
Move(int, int),
227+
Write(string),
228+
ChangeColor(int, int, int)
229+
};
230+
231+
// 创建不同类型的消息
232+
quit_msg : Message = Message::Quit;
233+
move_msg : Message = Message::Move(10, 20);
234+
write_msg : Message = Message::Write("Hello, World!");
235+
color_msg : Message = Message::ChangeColor(255, 128, 64);
236+
```
237+
238+
枚举类型可以作为函数参数和返回值使用,支持字符串连接操作,并且可以在控制台中正确显示。

0 commit comments

Comments
 (0)