Open main menu

Nim (programming language)

Nim (formerly named Nimrod) is an imperative, multi-paradigm, compiled programming language[7] designed and developed by Andreas Rumpf. It is designed to be "efficient, expressive, and elegant",[8] supporting metaprogramming, functional, message passing,[5] procedural, and object-oriented programming styles by providing several features such as compile time code generation, algebraic data types, a foreign function interface (FFI) with C and compiling to JavaScript, C and C++.

Paradigm multi-paradigm: compiled, concurrent, procedural, imperative, object-oriented
Designed by Andreas Rumpf
First appeared 2008; 10 years ago (2008)
Preview release
0.18.0[1] / 1 March 2018; 6 months ago (2018-03-01)
Typing discipline static,[2] strong,[3] inferred, structural
OS Cross-platform[4]
License MIT[5][6]
Filename extensions .nim
Influenced by
Ada, Modula-3, Lisp, C++, Object Pascal, Python, Oberon



Nim is statically typed.[9] It supports compile-time metaprogramming features such as syntactic macros and term rewriting macros.[10] Term rewriting macros enable library implementations of common data structures such as bignums and matrices to be implemented with an efficiency as if they were builtin language facilities.[11] Iterators are supported and can be used as first class entities[10] in the language as can functions, these features allow for functional programming to be used. Object-oriented programming is supported by inheritance and multiple dispatch. Functions can be generic and can also be overloaded, generics are further enhanced by the support for type classes. Operator overloading is also supported.[10] Nim includes tunable automatic garbage collection based on deferred reference counting with cycle detection.[12] In 2014, Andrew Binstock (editor-in-chief of Dr. Dobb's) said Nim (formerly known as Nimrod) "presents a most original design that straddles Pascal and Python and compiles to C code or JavaScript."[13] Today, Nim compiles to C++ too.


The initial development of Nim began in 2005 by Andreas Rumpf. The first version of the Nim compiler was written in Pascal using the Free Pascal compiler.[14] In 2008, a version of the compiler written in Nim was released.[15] The compiler is open source and is being developed by a group of volunteers in addition to Andreas Rumpf.[16]

Language designEdit

The syntax of Nim is similar to Python.

In details, it is influenced by:

In addition, Nim supports a Uniform Call Syntax[17] and identifier equality.[18]


The Nim compiler emits optimized C code and defers compiling to an external compiler[19] (many compilers are supported including Clang and GNU Compiler Collection (GCC)) to leverage their optimizing and portability abilities. The compiler can also emit C++, Objective-C and JavaScript code to allow easy interfacing with application programming interfaces (APIs) written in those languages.[7] This allows writing applications for iOS and Android.

The Nim compiler itself is written in the Nim programming language.[20]



Nimble[21] is the package manager used by Nim to package the Nim modules. It uses NimScript for the configuration. Nimble works on Git repositories as its primary source of packages. Its list of packages is stored in a JSON file which is freely accessible in the nim-lang/packages repository. This JSON file provides nimble with the required Git URL to clone the package and install it.


c2nim[22] helps to generate new bindings by translating Ansi C code to Nim code. The output is human-readable Nim code that is meant to be tweaked by hand after the translation process.


Choosenim installs Nim from official downloads and sources, enabling you to easily switch between stable and development compilers.[23]


Nimfix[24] is a tool that helps to convert old-style Nimrod code to Nim code. Nimfix is currently beta-quality.[25]


pas2nim[26] is a tool to translate Object Pascal wrappers to Nim code. It was used to translate the original Pascal sources of the Nim compiler. Only what maps easily to Nim is supported. Free Pascal/Delphi-style class or other "fancy" features are not supported. At this point the project is largely unmaintained.


py2nim[27] is a tool used for transpiling Python code into idiomatic Nim code. The project is in active development, with plans to extend the amount of Python code that can be entirely translated into Nim.


A Nim program can use any library which can be used in a C and C++ program. Language bindings exist for many libraries, for example GTK+2, SDL2, Cairo, OpenGL, WinAPI, zlib, libzip, OpenSSL and cURL.[28] Nim works with PostgreSQL, MySQL and SQLite databases. Nim can interface with the Lua[29] and Python interpreter.[30]


The following code examples are valid as of Nim 0.17.0. Because Nim has not had its 1.0 release, syntax and semantics may change in subsequent versions.

Hello worldEdit

The "Hello, World!" program in Nim:

echo("Hello, world!")
# You can call procedure without parentheses
echo "Hello, World!"

Reversing a stringEdit

A simple demonstration showing many of Nim's features.

proc reverse(s: string): string =
  result = "" # implicit result variable
  for i in countdown(s.high, 0):
    result.add s[i]

let str1 = "Reverse This!"
echo "Reversed: ", reverse(str1)

One of the more exotic features is the implicit result variable: every procedure in Nim with a non-void return type has an implicit result variable that represents the value that will be returned. In the for loop we see an invocation of countdown which is an iterator, if an iterator is omitted then the compiler will attempt to use an items iterator if one is defined for the type that was specified in the for loop.


Stropping is a method of explicitly marking letter sequences as having a special property. Stropping is not used in most modern languages – instead, keywords are reserved words and cannot be used as identifiers for variables or functions. Stropping allows the same letter sequence to be used both as a keyword and as an identifier, and simplifies parsing in that case – for example allowing a variable named if without clashing with the keyword if. In Nim, this is achieved via backticks, allowing any reserved word to be used as an identifier when inside backticks.[31]

var `type`: int


This is an example of metaprogramming in Nim using its template facilities.

template genType(name, fieldname: untyped, fieldtype: typedesc) =
    name = object
      fieldname: fieldtype

genType(Test, foo, int)

var x = Test(foo: 4566)
echo( # 4566

The genType is invoked at compile-time and a Test type is created.

Wrapping C functionsEdit

The following program demonstrates the ease with which existing C code can be directly used in Nim.

proc printf(formatstr: cstring) {.header: "<stdio.h>", varargs.}

printf("%s %d\n", "foo", 5)

In this code the well known printf function is imported into Nim and subsequently used.


The language has a bug tracker with wiki hosted by GitHub and a forum.[32][33] A presentation at O'Reilly Open Source Convention (OSCON) in 2015 took place.[34] O'Reilly Community: Essential Languages: Nim, Scala, Python.[35][36]

See alsoEdit


  1. ^ "Version 0.18.0 released". 2018-03-01. 
  2. ^ "Nim by example". GitHub. Retrieved 2014-07-20. 
  3. ^ Караджов, Захари; Станимиров, Борислав (2014). Метапрограмиране с Nimrod. VarnaConf (in Bulgarian). Retrieved 2014-07-27. 
  4. ^
  5. ^ a b "FAQ". Official website. Retrieved 2015-03-27. 
  6. ^ "copying.txt". Nim. GitHub. Retrieved 2015-03-27. 
  7. ^ a b Rumpf, Andreas (2014-02-11). "Nimrod: A new systems programming language". Dr. Dobb's Journal. Retrieved 2014-07-20. 
  8. ^ "The Nim Programming Language". Official website. Retrieved 2014-07-20. 
  9. ^ "Nim Syntax". akehrer. Retrieved 2015-01-05. 
  10. ^ a b c "Nim Manual". Official website. Retrieved 2014-07-20. 
  11. ^ "Strangeloop Nim presentation". Archived from the original on 2014-07-13. Retrieved 2015-04-30. 
  12. ^ "Nim's Garbage Collector". Nim documentation. Retrieved 2018-01-10. 
  13. ^ The Rise And Fall of Languages in 2013 By Andrew Binstock, 7 January 2014 Dr. Dobb's
  14. ^ "Nim Pascal Sources". Nim. GitHub. Retrieved 2013-04-05. 
  15. ^ "News". Official website. Retrieved 2016-06-11. 
  16. ^ "Contributors". Nim. GitHub. Retrieved 2013-04-05. 
  17. ^ Nim Manual:Method call syntax
  18. ^ Nim Manual:Identifier equality
  19. ^ Rumpf, Andreas (2014-01-15). Nimrod: A New Approach to Metaprogramming. InfoQ. Event occurs at 2:23. Retrieved 2014-07-20. 
  20. ^
  21. ^ Nimble
  22. ^ c2nim
  23. ^  Missing or empty |title= (help)
  24. ^  Missing or empty |title= (help)
  25. ^ .  Missing or empty |title= (help); External link in |publisher= (help);
  26. ^
  27. ^
  28. ^ "Nim Standard Library". Nim documentation. Archived from the original on 2015-04-06. Retrieved 2015-04-04. 
  29. ^
  30. ^
  31. ^
  32. ^ "Primary source code repository and bug tracker". GitHub. Retrieved 2015-05-04. 
  33. ^ "Nim Forum". Retrieved 2015-05-04. 
  34. ^ "Nim at OSCON 2015". OSCON. 2015-07-20. Retrieved 2015-05-04. 
  35. ^ "Essential Languages: Nim, Scala, Python". 
  36. ^ Presentation of Nim by Andreas Rumpf on OSCON 2015 on YouTube

External linksEdit