Trisha Shetty (Editor)

Nim (programming language)

Updated on
Edit
Like
Comment
Share on FacebookTweet on TwitterShare on LinkedInShare on Reddit
Designed by
  
Andreas Rumpf

OS
  
Cross-platform

Paradigm
  
multi-paradigm: compiled, concurrent, procedural, imperative, object-oriented

First appeared
  
2008; 9 years ago (2008)

Preview release
  
0.16.0 / 8 January 2017; 2 months ago (2017-01-08)

Typing discipline
  
static, strong, inferred, structural

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

Contents

Description

Nim is statically typed, with a simple syntax. 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 with an efficiency as if they would have been builtin language facilities. Iterators are supported and can be used as first class entities 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. Nim includes automatic garbage collection based on deferred reference counting with cycle detection. Andrew Binstock (editor-in-chief of Dr. Dobb's) says Nim (formerly known as Nimrod) "presents a most original design that straddles Pascal and Python and compiles to C code or JavaScript."

History

The initial development of Nim began in 2005 by Andreas Rumpf. The first version of the Nim compiler was written in Pascal. In 2008, 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.

Language design

The syntax of Nim is similar to Python.

In details, it is influenced by:

  • Modula-3: traced vs untraced pointers
  • Delphi: type safe bit sets (set of char)
  • Ada: subrange types, distinct type, safe variants / case objects
  • C++: Excessive overloading, generic programming
  • Python: Off-side rule
  • Lisp: Macro system, embrace the AST, homoiconicity
  • Oberon: The export marker
  • C#: Async / await, lambda macros
  • Go: Defer
  • In addition, Nim supports a Uniform Call Syntax and identifier equality.

    Compiler

    The Nim compiler emits optimized C code and defers compiling to an external compiler (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. This allows writing applications for iOS and Android.

    The Nim compiler itself is written in the Nim programming language.

    Nimble

    Nimble 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

    c2nim 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.

    Libraries

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

    Examples

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

    Hello world

    The "Hello, World!" program in Nim:

    Reversing a string

    A simple demonstration showing many of Nim's features.

    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.

    Metaprogramming

    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

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

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

    Community

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

    References

    Nim (programming language) Wikipedia