Modular programming of the Nim language

Source: Internet
Author: User

Objective

NIM supports dividing a large segment of the program into several modules

A module is a source code file

Each module has its own namespace

Modularity can play a role in encapsulation (information hiding) and step-up compilation

One module can get the symbol of another module through an import statement

The Nim language allows for a circular reference between modules,

Only top-level symbols marked with an asterisk (*) are exported to other modules.

The module name and file name are the same, the module name is named in the same way as the NIM programming language identifier

The compiler compiles the following rules for the module:

    • Compile the module according to import order

    • If there is a circular reference, then only the top-level symbol (the parsed symbol) is imported, and if the compiler finds an unknown identifier, it stops compiling

Take a look at the following example:

Code for Module A:

# module Atype t1* = int # module A exports the type ' T1 ' import B # The compiler starts parsing bproc main () = VA R i = P (3) # Works because B has been parsed completely heremain ()

Code for Module B:

# Module Bimport A # A is not parsed here! Only the already known symbols # of A is Imported.proc p* (x:a.t1): a.t1 = # This works because the compiler ha S already # added T1 to A ' s interface symbol table result = x + 1

Does it look very bad?!

Import statement

One or more modules can be imported via import

(If you import multiple modules, just follow the name of the module after import, and the module name is separated by commas)

You can exclude symbols in one or more modules with except

Take a look at the following sample code:

Import strutils except '% ', toupper# doesn ' t work then:echo "$"% "abc". ToUpper

Note: If an excluded identifier is not exported in the imported module, the Nim compiler will not give an alarm or an exception

Include statement

The include statement is completely different from the import statement,

The Include statement forces the compiler to "include" the source of a file in another file.

The include statement is useful when you need to split a file into multiple files

Include FileA, Fileb, Filec

The module name in the import statement

The module name in the import statement can be set alias

Import strutils as Su, sequtils as Quecho Su.format ("$", "Lalelu")

If you use an alias, the original module name will not work.

If a module is in a subdirectory

You can use the following three ways to import the module

Import Lib.pure.strutils, Lib/pure/os, "Lib/pure/times"

Note: Although the module is in a subdirectory, the module name does not contain a path

The following code is incorrect:

Import Lib.pure.strutilsecho Lib.pure.strutils

The following code settings do not make any sense

Import Lib.pure.strutils as Strutils

From...import ... Statement

If you want to import only the specified symbol for a particular module, you can use this statement

Look at the following code:

From strutils import '% ' echo "$"% "ABC" # But developers can also use the full qualifier to invoke this module in other ways: Echo strutils.replace ("abc", "A", "Z")

If you want to force the developer to use the full qualifier in the keynote module to invoke the symbol of the modulated module

So you can use the following method

Rom strutils import nil

Export statement

Let's take a look at the code for the following three modules

# module Btype myobject* = Object
# module Aimport bexport b.myobjectproc ' $ ' * (x:myobject): string = "My Object"
# module Cimport a# b.myobject has been imported implicitly Here:var X:myobjectecho ($x)

Module A exports the symbols in module B.

That way, module C doesn't have to import module B anymore.

Modular programming of the Nim language

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.