modules.txt 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. Modules
  2. =======
  3. Nim supports splitting a program into pieces by a module concept.
  4. Each module needs to be in its own file and has its own `namespace`:idx:.
  5. Modules enable `information hiding`:idx: and `separate compilation`:idx:.
  6. A module may gain access to symbols of another module by the `import`:idx:
  7. statement. `Recursive module dependencies`:idx: are allowed, but slightly
  8. subtle. Only top-level symbols that are marked with an asterisk (``*``) are
  9. exported. A valid module name can only be a valid Nim identifier (and thus its
  10. filename is ``identifier.nim``).
  11. The algorithm for compiling modules is:
  12. - compile the whole module as usual, following import statements recursively
  13. - if there is a cycle only import the already parsed symbols (that are
  14. exported); if an unknown identifier occurs then abort
  15. This is best illustrated by an example:
  16. .. code-block:: nim
  17. # Module A
  18. type
  19. T1* = int # Module A exports the type ``T1``
  20. import B # the compiler starts parsing B
  21. proc main() =
  22. var i = p(3) # works because B has been parsed completely here
  23. main()
  24. .. code-block:: nim
  25. # Module B
  26. import A # A is not parsed here! Only the already known symbols
  27. # of A are imported.
  28. proc p*(x: A.T1): A.T1 =
  29. # this works because the compiler has already
  30. # added T1 to A's interface symbol table
  31. result = x + 1
  32. Import statement
  33. ~~~~~~~~~~~~~~~~
  34. After the ``import`` statement a list of module names can follow or a single
  35. module name followed by an ``except`` list to prevent some symbols to be
  36. imported:
  37. .. code-block:: nim
  38. import strutils except `%`, toUpper
  39. # doesn't work then:
  40. echo "$1" % "abc".toUpper
  41. It is not checked that the ``except`` list is really exported from the module.
  42. This feature allows to compile against an older version of the module that
  43. does not export these identifiers.
  44. Include statement
  45. ~~~~~~~~~~~~~~~~~
  46. The ``include`` statement does something fundamentally different than
  47. importing a module: it merely includes the contents of a file. The ``include``
  48. statement is useful to split up a large module into several files:
  49. .. code-block:: nim
  50. include fileA, fileB, fileC
  51. Module names in imports
  52. ~~~~~~~~~~~~~~~~~~~~~~~
  53. A module alias can be introduced via the ``as`` keyword:
  54. .. code-block:: nim
  55. import strutils as su, sequtils as qu
  56. echo su.format("$1", "lalelu")
  57. The original module name is then not accessible. The
  58. notations ``path/to/module`` or ``path.to.module`` or ``"path/to/module"``
  59. can be used to refer to a module in subdirectories:
  60. .. code-block:: nim
  61. import lib.pure.strutils, lib/pure/os, "lib/pure/times"
  62. Note that the module name is still ``strutils`` and not ``lib.pure.strutils``
  63. and so one **cannot** do:
  64. .. code-block:: nim
  65. import lib.pure.strutils
  66. echo lib.pure.strutils
  67. Likewise the following does not make sense as the name is ``strutils`` already:
  68. .. code-block:: nim
  69. import lib.pure.strutils as strutils
  70. From import statement
  71. ~~~~~~~~~~~~~~~~~~~~~
  72. After the ``from`` statement a module name follows followed by
  73. an ``import`` to list the symbols one likes to use without explict
  74. full qualification:
  75. .. code-block:: nim
  76. from strutils import `%`
  77. echo "$1" % "abc"
  78. # always possible: full qualification:
  79. echo strutils.replace("abc", "a", "z")
  80. It's also possible to use ``from module import nil`` if one wants to import
  81. the module but wants to enforce fully qualified access to every symbol
  82. in ``module``.
  83. Export statement
  84. ~~~~~~~~~~~~~~~~
  85. An ``export`` statement can be used for symbol fowarding so that client
  86. modules don't need to import a module's dependencies:
  87. .. code-block:: nim
  88. # module B
  89. type MyObject* = object
  90. .. code-block:: nim
  91. # module A
  92. import B
  93. export B.MyObject
  94. proc `$`*(x: MyObject): string = "my object"
  95. .. code-block:: nim
  96. # module C
  97. import A
  98. # B.MyObject has been imported implicitly here:
  99. var x: MyObject
  100. echo $x
  101. Note on paths
  102. -----------
  103. In module related statements, if any part of the module name /
  104. path begins with a number, you may have to quote it in double quotes.
  105. In the following example, it would be seen as a literal number '3.0' of type
  106. 'float64' if not quoted, if uncertain - quote it:
  107. .. code-block:: nim
  108. import "gfx/3d/somemodule"
  109. Scope rules
  110. -----------
  111. Identifiers are valid from the point of their declaration until the end of
  112. the block in which the declaration occurred. The range where the identifier
  113. is known is the scope of the identifier. The exact scope of an
  114. identifier depends on the way it was declared.
  115. Block scope
  116. ~~~~~~~~~~~
  117. The *scope* of a variable declared in the declaration part of a block
  118. is valid from the point of declaration until the end of the block. If a
  119. block contains a second block, in which the identifier is redeclared,
  120. then inside this block, the second declaration will be valid. Upon
  121. leaving the inner block, the first declaration is valid again. An
  122. identifier cannot be redefined in the same block, except if valid for
  123. procedure or iterator overloading purposes.
  124. Tuple or object scope
  125. ~~~~~~~~~~~~~~~~~~~~~
  126. The field identifiers inside a tuple or object definition are valid in the
  127. following places:
  128. * To the end of the tuple/object definition.
  129. * Field designators of a variable of the given tuple/object type.
  130. * In all descendant types of the object type.
  131. Module scope
  132. ~~~~~~~~~~~~
  133. All identifiers of a module are valid from the point of declaration until
  134. the end of the module. Identifiers from indirectly dependent modules are *not*
  135. available. The `system`:idx: module is automatically imported in every module.
  136. If a module imports an identifier by two different modules, each occurrence of
  137. the identifier has to be qualified, unless it is an overloaded procedure or
  138. iterator in which case the overloading resolution takes place:
  139. .. code-block:: nim
  140. # Module A
  141. var x*: string
  142. .. code-block:: nim
  143. # Module B
  144. var x*: int
  145. .. code-block:: nim
  146. # Module C
  147. import A, B
  148. write(stdout, x) # error: x is ambiguous
  149. write(stdout, A.x) # no error: qualifier used
  150. var x = 4
  151. write(stdout, x) # not ambiguous: uses the module C's x