Nim (programming language)
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 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, and LLVM.
History
Andreas Rumpf is the designer and original implementer of Nim. He received a diploma in computer science from the University of Kaiserslautern-Landau, 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 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:
-
openarrayRepresents arrays of different sizes, sequences, and strings -
SomeSignedIntRepresents all the signed integer types -
SomeIntegerRepresents all the Integer types, signed or not -
SomeOrdinalRepresents all the basic countable and ordered types, except of non integer number
- Let's declare a function that takes any type of number and displays its double
- In Nim functions with side effect are called "proc"
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, and precise that it can only be an Ordinal
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 =.T
echo twiceIfIsNumber
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, 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
For example, each of these lines print "hello world", just with different syntax:
echo "hello world"
echo
"hello world".echo
"hello world".echo
echo
"hello".echo
"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 namedif, 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
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++, MinGW, and GNU Compiler Collection. The Nim compiler can also emit C++, Objective-C, and JavaScript code to allow easy interfacing with application programming interfaces 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 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 annotatingpragmas or by using--mm:orc. -
--mm:orc– Same as--mm:arcbut adds a cycle collector 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.