@@ -87,6 +87,7 @@ by using any of the following commands in a terminal:
87
87
88
88
* [ Module imports] ( #module-imports )
89
89
* [ Selective imports] ( #selective-imports )
90
+ * [ Module hierarchy] ( #module-hierarchy )
90
91
* [ Module import aliasing] ( #module-import-aliasing )
91
92
* [ Statements & expressions] ( #statements--expressions )
92
93
* [ If] ( #if )
@@ -1577,6 +1578,54 @@ println('Name: ${name}')
1577
1578
current_os := user_os()
1578
1579
println('Your OS is ${current_os}.')
1579
1580
```
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
+
1580
1629
1581
1630
### Module import aliasing
1582
1631
0 commit comments