Nim (programming language)

For other uses, see Nim (disambiguation).
Nim
Paradigm compiled, concurrent, procedural, imperative, object-oriented
Designed by Andreas Rumpf
First appeared 2008
0.11.0[1] / 2015-04-30
static,[2] strong,[3] inferred, structural
OS Windows, OS X, Linux, FreeBSD, NetBSD
License MIT License[4][5]
Website nim-lang.org

Nim (formerly known as Nimrod) is an imperative, multi-paradigm, compiled programming language[6] designed and developed by Andreas Rumpf. It is designed to be an "efficient, expressive, and elegant" programming language,[7] supporting metaprogramming,[8] functional, message passing,[9] procedural, and object-oriented programming styles by providing several features such as compile time code generation, algebraic data types, an elegant FFI with C and compilation to Javascript.[10][11][12]

The Nim compiler was initially written in Pascal,[13] in 2008[1] a version of the compiler written in Nim was released. The compiler is open source and is being developed by a group of volunteers in addition to Andreas Rumpf.[14] The compiler generates optimized C code and defers compilation to an external compiler[15] (a large range of compilers including clang and GCC are supported) to leverage their optimization and portability capabilities. The compiler can also generate C++ and Objective C code to allow for easy interfacing with APIs written in those languages,[6] this in turn allows Nim to be used to write iOS as well as Android applications.[16][17]

Description

Nim is a statically typed programming language with a simple syntax.[18] Nim supports compile-time metaprogramming features such as syntactic macros and term rewriting macros.[19][20] Term rewriting macros enable library implementations of common data structures such as bignums and matrixes to be implemented with an efficiency as if they would have been builtin language facilities. [21]Iterators are supported and can be used as first class entities[20] 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.[20] Nim includes automatic garbage collection based on deferred reference counting with cycle detection.[22]

Examples

The following code examples are valid as of Nim 0.10.2. Syntax and semantics may change in subsequent versions.

Hello world

echo "Hello World!"

Reversing a string

proc reverse(s: string): string =
  result = "" # implicit result variable
  for i in countdown(high(s), 0):
    result.add s[i]
 
var str1 = "Reverse This!"
echo "Reversed: ", reverse(str1)

This example shows many of Nim's features, one of the most exotic ones 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.

Metaprogramming

template genType(name, fieldname: expr, fieldtype: typedesc) =
  type
    name = object
      fieldname: fieldtype
 
genType(Test, foo, int)
 
var x = Test(foo: 4566)
echo(x.foo) # 4566

This is an example of metaprogramming in Nim using its template facilities. The genType is invoked at compile-time and a Test type is created.

Wrapping C functions

proc printf(formatstr: cstring)
  {.header: "<stdio.h>", varargs.}
 
printf("%s %d\n", "foo", 5)

Existing C code can directly be used in Nim. In this code the well known printf function is imported into Nim and subsequently used.[23]

Libraries

A Nim program can use any library which can be used in a C program. There are existing bindings for many libraries, for example GTK+2, SDL2, Cairo, OpenGL, WinAPI, zlib, libzip, OpenSSL and cURL.[24] Nim works with PostgreSQL, MySQL and SQLite databases. Nim can interface with the Lua and Python interpreter. The tool c2nim helps to generate new bindings from C code.

References

  1. 1.0 1.1 "News". Official website. Retrieved 2015-04-30.
  2. "Nim by example". GitHub. Retrieved 2014-07-20.
  3. Караджов, Захари; Станимиров, Борислав (2014). Метапрограмиране с Nimrod. VarnaConf (in Bulgarian). Retrieved 2014-07-27.
  4. "FAQ". Official website. Retrieved 2015-03-27.
  5. "copying.txt". Nim. GitHub. Retrieved 2015-03-27.
  6. 6.0 6.1 Rumpf, Andreas (2014-02-11). "Nimrod: A new systems programming language". Dr. Dobb's Journal. Retrieved 2014-07-20.
  7. "The Nim Programming Language". Official website. Retrieved 2014-07-20.
  8. "Nim Programming Language Gaining Traction". Slashdot anonymous poster. Retrieved 15 Feb 2014.
  9. "FAQ". Official website. Retrieved 2013-04-05.
  10. "What's so special about Nim?". Hookrace blog. Retrieved 2015-03-27.
  11. "What makes Nim practical?". Reddit. Retrieved 2015-03-27.
  12. "I missed Nim". Reddit. Retrieved 2015-03-27.
  13. "Nim Pascal Sources". Nim. GitHub. Retrieved 2013-04-05.
  14. "Contributors". Nim. GitHub. Retrieved 2013-04-05.
  15. Rumpf, Andreas (2014-01-15). Nimrod: A New Approach to Metaprogramming. InfoQ. Event occurs at 2:23. Retrieved 2014-07-20.
  16. Hankiewicz, Grzegorz Adam (2014-03-10). "Nimrod for cross platform software". Rants from the Ballmer Peak. GitHub. Retrieved 2014-07-20.
  17. "Nimrod-on-android failure". 2012-07-28. Retrieved 2015-02-28.
  18. "Nim Syntax". akehrer. Retrieved 2015-03-27.
  19. "Syntactic Macros". Wikipedia. Retrieved 2015-03-27.
  20. 20.0 20.1 20.2 "Nim Manual". Official website. Retrieved 2014-07-20.
  21. "Strangeloop Nim presentation". Retrieved 2015-04-30.
  22. "Nim's Garbage Collector". Nim documentation. Retrieved 2015-04-03.
  23. "What is special about Nim?". HookRace. 2015-01-01. Retrieved 2015-02-17.
  24. "Nim Standard Library". Nim documentation. Retrieved 2015-04-04.

External links