Skip to content

Commit 26837d4

Browse files
authored
docs: add module hierarchy section (#22919)
1 parent ad24dbd commit 26837d4

File tree

1 file changed

+49
-0
lines changed

1 file changed

+49
-0
lines changed

doc/docs.md

+49
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ by using any of the following commands in a terminal:
8787

8888
* [Module imports](#module-imports)
8989
* [Selective imports](#selective-imports)
90+
* [Module hierarchy](#module-hierarchy)
9091
* [Module import aliasing](#module-import-aliasing)
9192
* [Statements & expressions](#statements--expressions)
9293
* [If](#if)
@@ -1577,6 +1578,54 @@ println('Name: ${name}')
15771578
current_os := user_os()
15781579
println('Your OS is ${current_os}.')
15791580
```
1581+
### Module hierarchy
1582+
1583+
> [!NOTE]
1584+
> This section is valid when .v files are not in the project's root directory.
1585+
1586+
Modules names in .v files, must match the name of their directory.
1587+
1588+
A .v file `./abc/source.v` must start with `module abc`. All .v files in this directory
1589+
belong to the same module `abc`. They should also start with `module abc`.
1590+
1591+
If you have `abc/def/`, and .v files in both folders, you can `import abc`, but you will have
1592+
to `import abc.def` too, to get to the symbols in the subfolder. It is independent.
1593+
1594+
In `module name` statement, name never repeats directory's hierarchy, but only its directory.
1595+
So in `abc/def/source.v` the first line will be `module def`, and not `module abc.def`.
1596+
1597+
`import module_name` statements must respect file hierarchy, you cannot `import def`, only
1598+
`abc.def`
1599+
1600+
Refering to a module symbol such as a function or const, only needs module name as prefix:
1601+
1602+
```v ignore
1603+
module def
1604+
1605+
// func is a dummy example function.
1606+
pub fn func() {
1607+
println('func')
1608+
}
1609+
```
1610+
1611+
can be called like this:
1612+
1613+
```v ignore
1614+
module main
1615+
1616+
import def
1617+
1618+
fn main() {
1619+
def.func()
1620+
}
1621+
```
1622+
1623+
A function, located in `abc/def/source.v`, is called with `def.func()`, not `abc.def.func()`
1624+
1625+
This always implies a *single prefix*, whatever sub-module depth. This behavior flattens
1626+
modules/sub-modules hierarchy. Should you have two modules with the same name in different
1627+
directories, then you should use Module import aliasing (see below).
1628+
15801629

15811630
### Module import aliasing
15821631

0 commit comments

Comments
 (0)