Skip to content

Commit

Permalink
docs: add module hierarchy section (#22919)
Browse files Browse the repository at this point in the history
  • Loading branch information
Bruno-Vdr authored Nov 20, 2024
1 parent ad24dbd commit 26837d4
Showing 1 changed file with 49 additions and 0 deletions.
49 changes: 49 additions & 0 deletions doc/docs.md
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ by using any of the following commands in a terminal:

* [Module imports](#module-imports)
* [Selective imports](#selective-imports)
* [Module hierarchy](#module-hierarchy)
* [Module import aliasing](#module-import-aliasing)
* [Statements & expressions](#statements--expressions)
* [If](#if)
Expand Down Expand Up @@ -1577,6 +1578,54 @@ println('Name: ${name}')
current_os := user_os()
println('Your OS is ${current_os}.')
```
### Module hierarchy

> [!NOTE]
> This section is valid when .v files are not in the project's root directory.
Modules names in .v files, must match the name of their directory.

A .v file `./abc/source.v` must start with `module abc`. All .v files in this directory
belong to the same module `abc`. They should also start with `module abc`.

If you have `abc/def/`, and .v files in both folders, you can `import abc`, but you will have
to `import abc.def` too, to get to the symbols in the subfolder. It is independent.

In `module name` statement, name never repeats directory's hierarchy, but only its directory.
So in `abc/def/source.v` the first line will be `module def`, and not `module abc.def`.

`import module_name` statements must respect file hierarchy, you cannot `import def`, only
`abc.def`

Refering to a module symbol such as a function or const, only needs module name as prefix:

```v ignore
module def
// func is a dummy example function.
pub fn func() {
println('func')
}
```

can be called like this:

```v ignore
module main
import def
fn main() {
def.func()
}
```

A function, located in `abc/def/source.v`, is called with `def.func()`, not `abc.def.func()`

This always implies a *single prefix*, whatever sub-module depth. This behavior flattens
modules/sub-modules hierarchy. Should you have two modules with the same name in different
directories, then you should use Module import aliasing (see below).


### Module import aliasing

Expand Down

0 comments on commit 26837d4

Please sign in to comment.