Skip to content

Commit 75f1fa7

Browse files
committed
Modules page
1 parent 9c9e6c3 commit 75f1fa7

2 files changed

Lines changed: 82 additions & 0 deletions

File tree

docs/modules.md

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
# Modules
2+
3+
Modules are a way to organize your code into separate files.
4+
5+
## File Structure
6+
7+
As long as you have a `main.rue` file in the root of your project, the compiler will automatically include all of the `.rue` files adjacent to it (including subdirectories) in the compilation.
8+
9+
All files are modules and can reference each other by name:
10+
11+
1. Each file includes every other adjacent file or directory in its scope.
12+
2. Each directory's module automatically exports all of its children.
13+
3. Each file has access to the parent directory's module with `super`.
14+
15+
If there is no `main.rue` file, then each file ending in `.rue` will be compiled completely independently, rather than as a single program.
16+
17+
## Paths
18+
19+
You can reference exported symbols and types from other modules by using the module name followed by the path to the symbol or type.
20+
21+
```rue
22+
fn main() -> String {
23+
utils::greet("Alice")
24+
}
25+
```
26+
27+
In this case, we are referencing the `greet` function from the `utils` module.
28+
29+
## Imports
30+
31+
Instead of referencing by path every time, you can import symbols and types from other modules into the current scope so that they can be referenced directly.
32+
33+
```rue
34+
import utils::greet;
35+
36+
fn main() -> String {
37+
greet("Alice")
38+
}
39+
```
40+
41+
You can import multiple paths:
42+
43+
```rue
44+
import utils::{greet, exclaim};
45+
46+
fn main() -> String {
47+
exclaim(greet("Alice"))
48+
}
49+
```
50+
51+
You can also import all symbols and types from a module by using the `*` wildcard:
52+
53+
```rue
54+
import utils::*;
55+
56+
fn main() -> String {
57+
exclaim(greet("Alice"))
58+
}
59+
```
60+
61+
Finally, it's possible to re-export things that you import:
62+
63+
```rue
64+
export utils::*;
65+
```
66+
67+
## Module Block
68+
69+
You can create a module by using the `mod` keyword followed by the name of the module. This is here for completeness more than anything, since most of the time you'll want to use multiple files instead.
70+
71+
```rue
72+
mod utils {
73+
export fn add(a: Int, b: Int) -> Int {
74+
a + b
75+
}
76+
}
77+
78+
fn main() -> Int {
79+
utils::add(42, 34)
80+
}
81+
```

sidebars.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ const sidebars: SidebarsConfig = {
88
label: "Tutorials",
99
items: ["tutorials/password", "tutorials/signature"],
1010
},
11+
"modules",
1112
"functions",
1213
"control-flow",
1314
"bindings",

0 commit comments

Comments
 (0)