Nim is a general-purpose, multi-paradigm, statically typed, compiled, high-level system programming language. It was designed and developed by a team led by Andreas Rumpf. Nim aims to be "efficient, expressive, and elegant", and supports metaprogramming, functional, message passing, procedural, and object-oriented programming paradigms. Nim includes features such as compile-time code generation, algebraic data types, and a foreign function interface (FFI) for interfacing with C, C++, Objective-C, and JavaScript. It also supports compilation to these same languages as intermediate representations.

Description Nim is statically typed. It supports compile-time metaprogramming features such as syntactic macros and term rewriting macros. Term rewriting macros enable library implementations of common data structures, such as bignums and matrices, to be implemented efficiently and with syntactic integration, as if they were built-in language facilities. Iterators are supported and can be used as first class entities, as can functions, allowing for the use of functional programming methods. Object-oriented programming is supported by inheritance and multiple dispatch. Functions can be generic and overloaded, and generics are further enhanced by Nim's support for type classes. Operator overloading is also supported. Nim includes multiple tunable memory management strategies, including tracing garbage collection, reference counting, and fully manual systems, with the default being deterministic reference counting with optimizations via move semantics and cycle collection via trial deletion.

, Nim compiles to C, C++, JavaScript, Objective-C,Nim Compiler User Guide and LLVM.

History

Branch Version Release date 0.x 2014-12-29 2015-05-04 2015-10-27 2016-01-18 2016-06-09 2016-10-23 2017-01-08 2017-09-07 2018-03-01 2019-05-13 2019-06-17 1.0 2019-09-23 2020-10-27 1.2 2020-04-03 2022-02-09 1.4 2020-10-16 2021-05-25 1.6 2021-10-19 2024-04-16 2.0 2023-08-01 2025-04-22 2.2 2024-10-02 2026-09-08 For each 0.x branch, only the latest point release is listed. For later branches, the first and the latest point release is listed.

Andreas Rumpf is the designer and original implementer of Nim. He received a diploma in computer science from the Technical University of Kaiserslautern, Germany. His research interests include hard real-time systems, embedded systems, compiler construction and artificial intelligence.

Nim's initial development was started in 2005 under the name Nimrod and was made public in 2008.

The first version of the Nim compiler was written in Pascal using the Free Pascal compiler. In 2008, a version of the compiler written in Nim was released. The compiler is free and open-source software, and is being developed by a community of volunteers working with Andreas Rumpf. The language was officially renamed from Nimrod to Nim with the release of version 0.10.2 in December 2014. On 23 September 2019, version 1.0 of Nim was released, signifying the maturing of the language and its toolchain. On 1 August 2023, version 2.0 of Nim was released, signifying the completion, stabilization of, and switch to the ARC/ORC memory model.

Language design

Syntax The syntax of Nim resembles that of Python. Code blocks and nesting statements are identified through use of whitespace, according to the offside-rule. Many keywords are identical to their Python equivalents, which are mostly English keywords, whereas other programming languages usually use punctuation. With the goal of improving upon its influence languages, even though Nim supports indentation-based syntax like Python, it introduced additional flexibility. For example, a single statement may span multiple lines if a comma or binary operator is at the end of each line. Nim also supports user-defined operators.

Unlike Python, Nim implements (native) static typing. Nim's type system allows for easy type conversion, casting, and provides syntax for generic programming. Nim notably provides type classes which can stand in for multiple types, and provides several such type classes 'out of the box'. Type classes allow working with several types as if they were a single type. For example:

openarray Represents arrays of different sizes, sequences, and strings SomeSignedInt Represents all the signed integer types SomeInteger Represents all the Integer types, signed or not SomeOrdinal Represents all the basic countable and ordered types, except of non integer number

This code sample demonstrates the use of typeclasses in Nim:

# Let's declare a function that takes any type of number and displays its double # In Nim functions with side effect are called "proc" proc timesTwo(i: SomeNumber) = echo i * 2

# Let's write another function that takes any ordinal type, and returns # the double of the input in its original type, if it is a number; # or returns the input itself otherwise. # We use a generic Type(T), and precise that it can only be an Ordinal func twiceIfIsNumber[T: SomeOrdinal](i: T): T = when T is SomeNumber: # A `when` is an `if` evaluated during compile time result = i * 2 # You can also write `return i * 2` else: # If the Ordinal is not a number it is converted to int, # multiplied by two, and reconverted to its based type result = (i.int * 2).T

echo twiceIfIsNumber(67) # Passes an int to the function echo twiceIfIsNumber(67u8) # Passes an uint8 echo twiceIfIsNumber(true) # Passes a bool (Which is also an Ordinal)

Influence According to the language creator, Nim was conceived to combine the best parts of Ada typing system, Python flexibility, and powerful Lisp macro system.

Nim was influenced by specific characteristics of existing languages, including the following:

Modula-3: traced vs untraced pointers Object Pascal: type safe bit sets (set of char), case statement syntax, various type names and filenames in the standard library Ada: subrange types, distinct type, safe variants – case objects C++: operator overloading, generic programming Python: Off-side rule Lisp: Macro system, AST manipulation, homoiconicity Oberon: export marker C#: async/await, lambda macros ParaSail: pointer-free programming

Uniform function call syntax Nim supports uniform function call syntax (UFCS) and identifier equality, which provides a large degree of flexibility in use.

For example, each of these lines print "hello world", just with different syntax: echo "hello world" echo("hello world") "hello world".echo() "hello world".echo echo("hello", " world") "hello".echo(" world") "hello".echo " world"

Identifier equality

Nim is almost fully style-insensitive; two identifiers are considered equal if they only differ by capitalization and underscores, as long as the first characters are identical. This is to enable a mixture of styles across libraries: one user can write a library using snake_case as a convention, and it can be used by a different user in a camelCase style without issue.const useHttps = true assert useHttps == useHttps assert useHTTPS == useHttps assert use_https == useHttps

Stropping The stropping feature allows the use of any name for variables or functions, even when the names are reserved words for keywords. An example of stropping is the ability to define a variable named if, without clashing with the keyword if. Nim's implementation of this is achieved via backticks, allowing any reserved word to be used as an identifier.

type Type = object `int`: int

let `object` = Type(`int`: 9) assert `object` is Type assert `object`.`int` == 9

var `var` = 42 let `let` = 8 assert `var` + `let` == 50

const `assert` = true assert `assert`

Compiler The Nim compiler emits fast, optimized C code by default. It defers compiling-to-object code to an external C compiler to leverage existing compiler optimization and portability. Many C compilers are supported, including Clang, Microsoft Visual C++ (MSVC), MinGW, and GNU Compiler Collection (GCC). The Nim compiler can also emit C++, Objective-C, and JavaScript code to allow easy interfacing with application programming interfaces (APIs) written in those languages; developers can simply write in Nim, then compile to any supported language. This also allows writing applications for iOS and Android. There is also an unofficial LLVM backend, allowing use of the Nim compiler in a stand-alone way.

The Nim compiler is self-hosting, meaning it is written in the Nim language. The compiler supports cross-compiling, so it is able to compile software for any of the supported operating systems, no matter the development machine. This is useful for compiling applications for embedded systems, and for uncommon and obscure computer architectures.

Compiler options

By default, the Nim compiler creates a debug build. With the option -d:release a release build can be created, which is optimized for speed and contains fewer runtime checks. With the option -d:danger all runtime checks can be disabled, if maximum speed is desired.

Memory management

Nim supports multiple memory management strategies, including the following: --mm:arc – Automatic reference counting (ARC) with move semantics optimizations, offers a shared heap. It offers fully deterministic performance for hard realtime systems. Reference cycles may cause memory leaks: these may be dealt with by manually annotating {.acyclic.} pragmas or by using --mm:orc. --mm:orc – Same as --mm:arc but adds a cycle collector (the "O") based on "trial deletion". The cycle collector only analyzes types if they are potentially cyclic. --mm:refc – Standard deferred reference counting based garbage collector with a simple mark-and-sweep backup GC in order to collect cycles. Heaps are thread-local. --mm:markAndSweep – Simple mark-and-sweep based garbage collector. Heaps are thread-local. --mm:boehm – Boehm based garbage collector, it offers a shared heap. --mm:go – Go's garbage collector, useful for interoperability with Go. Offers a shared heap. --mm:none – No memory management strategy nor a garbage collector. Allocated memory is simply never freed, unless manually freed by the developer's code.

As of Nim 2.0, ORC is the default GC.

Development tools

Bundled Many tools are bundled with the Nim install package, including:

Nimble Nimble is the standard package manager used by Nim to package Nim modules. It was initially developed by Dominik Picheta, who is also a core Nim developer. Nimble has been included as Nim's official package manager since 27 October 2015, the v0.12.0 release.

Nimble packages are defined by .nimble files, which contain information about the package version, author, license, description, dependencies, and more. These files support a limited subset of the Nim syntax called NimScript, with the main limitation being the access to the FFI. These scripts allow changing of test procedure, or for custom tasks to be written.

The list of packages is stored in a JavaScript Object Notation (JSON) file which is freely accessible in the nim-lang/packages repository on GitHub. This JSON file provides Nimble with a mapping between the names of packages and their Git or Mercurial repository URLs.

Nimble comes with the Nim compiler. Thus, it is possible to test the Nimble environment by running: nimble -v. This command will reveal the version number, compiling date and time, and Git hash of nimble. Nimble uses the Git package, which must be available for Nimble to function properly. The Nimble command-line is used as an interface for installing, removing (uninstalling), and upgrading–patching module packages.

c2nim c2nim is a source-to-source compiler (transcompiler or transpiler) meant to be used on C/C++ headers to help generate new Nim bindings. The output is human-readable Nim code that is meant to be edited by hand after the translation process.

koch koch is a maintenance script that is used to build Nim, and provide HTML documentation.

nimgrep