|
Oktatás * Programozás 1 * Szkriptnyelvek Teaching * Programming 1 (BI) Félévek Linkek * kalendárium |
Nim2 /
Write and use your own moduleSay you want to put some procedures in a separate file (module). How to do it and how to use that module? Let's see a simple example. It's very similar to Python. $ ls -al total 16 drwxr-xr-x 2 jabba jabba 4096 ápr 4 17.35 . drwxr-xr-x 27 jabba jabba 4096 ápr 4 16.31 .. -rwxr--r-- 1 jabba jabba 408 ápr 4 17.30 main.nim -rw-r--r-- 1 jabba jabba 141 ápr 4 17.26 mymath.nim
# this is private in the module proc hello() = echo "hello world" # the '*' indicates that it's public func twice*(n: int): int = 2 * n
import mymath proc main() = echo twice(5) # 10; everything is imported from the module that is public echo mymath.twice(5) # 10; you can use the module name (qualified access) if you want # hello() # ERROR: not visible # ############################################################################ when isMainModule: main() How to compile: $ nim c main.nim That is, you need to compile just the main file. The compiler will find and compile the dependencies for you. |
![]() Blogjaim, hobbi projektjeim * The Ubuntu Incident [ edit ] |