Skip to content

Commit fcbd01f

Browse files
committed
Add module docs
1 parent 4d6123b commit fcbd01f

File tree

3 files changed

+193
-2
lines changed

3 files changed

+193
-2
lines changed

docs/guide/language/_meta.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,5 @@
55
"enum-match",
66
"class-object",
77
"error-handling",
8-
"data-validation"
8+
"module"
99
]

docs/guide/language/data-validation.md

Lines changed: 0 additions & 1 deletion
This file was deleted.

docs/guide/language/module.mdx

Lines changed: 192 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,192 @@
1+
import { Badge, Tab, Tabs } from "rspress/theme";
2+
3+
# Modules
4+
5+
AIScript's module system allows you to organize your code into reusable, self-contained units. This helps maintain clean code structure, prevents naming conflicts, and enables efficient code sharing across your application.
6+
7+
## Importing Modules
8+
9+
Use the `use` keyword to import modules:
10+
11+
```rust
12+
// Import a standard library module
13+
use std.math;
14+
use std.http;
15+
16+
// Import a local module
17+
use my_app.utils;
18+
```
19+
20+
## Using Imported Modules
21+
22+
After importing a module, you can access its functions, classes, and variables using dot notation:
23+
24+
```rust
25+
use std.math;
26+
27+
// Use functions from the math module
28+
let result = math.sin(0.5) + math.cos(0.5);
29+
print(result);
30+
31+
// Constants are also available
32+
print(math.PI); // 3.14159...
33+
```
34+
35+
## Creating and Export Modules
36+
37+
In AIScript, each file is automatically a module. The name of the module corresponds to its file path relative to your project root:
38+
39+
<Tabs>
40+
<Tab label="string_helpers.ai">
41+
42+
```rust
43+
pub fn pluralize(word: str) -> str {
44+
// Simple pluralization logic
45+
if word.ends_with("s") {
46+
return word + "es";
47+
}
48+
return word + "s";
49+
}
50+
51+
// Private helper function (not exported)
52+
fn is_vowel(char: str) -> bool {
53+
return char.to_lowercase() in ["a", "e", "i", "o", "u"];
54+
}
55+
```
56+
</Tab>
57+
</Tabs>
58+
59+
Use `pub` keyword to make items public and available for import in other modules.
60+
61+
## Importing and Using Your Modules
62+
63+
```rust
64+
// Import your module
65+
use string_helpers;
66+
67+
let word = "apple";
68+
let plural = string_helpers.pluralize(word);
69+
print(plural); // "apples"
70+
71+
// The private is_vowel function is not accessible
72+
// string_helpers.is_vowel("a"); // Error: function is not exported
73+
```
74+
75+
## Module Hierarchies <Badge text="Not supported yet" type="warning" />
76+
77+
AIScript supports hierarchical module structures:
78+
79+
```
80+
project/
81+
├── main.ai
82+
├── models/
83+
│ ├── user.ai
84+
│ └── product.ai
85+
└── utils/
86+
├── validation.ai
87+
└── formatting.ai
88+
```
89+
90+
You can import modules from this hierarchy:
91+
92+
```rust
93+
// In main.ai
94+
use models.user;
95+
use models.product;
96+
use utils.validation;
97+
use utils.formatting;
98+
99+
let new_user = user.User("Alice", "alice@example.com");
100+
```
101+
102+
:::warning Circular Dependencies
103+
104+
AIScript also has circular dependencies issue:
105+
106+
```rust
107+
// In module_a.ai
108+
use module_b;
109+
110+
pub fn function_a() {
111+
print("Function A");
112+
module_b.function_b();
113+
}
114+
115+
// In module_b.ai
116+
use module_a;
117+
118+
pub fn function_b() {
119+
print("Function B");
120+
}
121+
```
122+
123+
AIScript cannot fix circular dependencies automatically, you should refactor your code to avoid them.
124+
125+
:::
126+
127+
## Standard Library Modules
128+
129+
AIScript comes with a rich standard library organized into modules:
130+
131+
```rust
132+
// Math operations
133+
use std.math;
134+
print(math.sqrt(16)); // 4
135+
136+
// File system operations
137+
use std.fs;
138+
fs.write_file("output.txt", "Hello, world!");
139+
140+
// HTTP client and server
141+
use std.http;
142+
let response = http.get("https://api.example.com/data");
143+
144+
// Date and time
145+
use std.time;
146+
print(time.now());
147+
148+
// JSON handling
149+
use std.serde;
150+
let parsed = srde.from_str('{"name": "AIScript"}');
151+
152+
// Database operations
153+
use std.db.pg;
154+
let users = pg.query("SELECT * FROM users");
155+
```
156+
157+
## Module Aliases <Badge text="Not supported yet" type="warning" />
158+
159+
Use the `as` keyword to create aliases for imported modules or items:
160+
161+
```rust
162+
use std.collections.HashMap as Map;
163+
use utils.very_long_module_name as short;
164+
165+
let map = Map();
166+
short.do_something();
167+
```
168+
169+
170+
## Dynamic Module Loading
171+
172+
AIScript supports dynamic module loading for advanced use cases:
173+
174+
```rust
175+
fn load_module(module_name: str) -> object {
176+
return import(module_name);
177+
}
178+
179+
let module = load_module("utils.helpers");
180+
module.function();
181+
```
182+
183+
## Best Practices
184+
185+
1. **Organize by feature**: Group related functionality together rather than organizing by type (e.g., models, utils)
186+
2. **Keep modules focused**: Each module should have a single responsibility
187+
3. **Minimize public API**: Only export what is necessary
188+
4. **Use clear naming**: Module names should clearly indicate their purpose
189+
5. **Avoid circular dependencies**: Restructure your code to minimize circular references
190+
6. **Document public API**: Add comments or docstrings to explain exported functions and classes
191+
192+
AIScript's module system strikes a balance between simplicity and power, allowing you to organize your code effectively while maintaining a clean development experience.

0 commit comments

Comments
 (0)