Recent Changes - Search:

Oktatás

* Programozás 1
  + feladatsor
  + GitHub oldal

* Szkriptnyelvek
  + feladatsor
  + quick link

Teaching

* Programming 1 (BI)
  ◇ exercises
  ◇ quick link

teaching assets


Félévek

* 2025/26/2
* archívum


Linkek

* kalendárium
   - munkaszüneti napok '20
* tételsorok
* jegyzetek
* szakdolgozat / PhD
* ösztöndíjak
* certificates
* C lang.
* C++
* C#
* Clojure
* D lang.
* Java
* Nim
* Nim2
* Scala


[ edit | logout ]
[ sandbox | passwd ]

Write and use your own module

Say 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

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

main.nim :

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.

Cloud City

  

Blogjaim, hobbi projektjeim

* The Ubuntu Incident
* Python Adventures
* @GitHub
* heroku
* extra
* haladó Python
* YouTube listák


Debrecen | la France


[ edit ]

Edit - History - Print *** Report - Recent Changes - Search
Page last modified on 2026 April 04, 22:56