Nim (programming language)

Nim is an imperative, general-purpose, multi-paradigm, statically typed, systems, 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, C++, Objective-C, and JavaScript, and compiling to C, C++, Objective-C, and JavaScript.

Nim
Nim-logo.png
The Nim crown logo
ParadigmsMulti-paradigm: compiled, concurrent, Procedural, Imperative, Functional, Object-oriented
Designed byAndreas Rumpf
First appeared2008; 12 years ago (2008)
Stable release
1.4.0[1] / 16 October 2020; 6 days ago (2020-10-16)
Typing disciplineStatic,[2] strong,[3] inferred, structural
PlatformIA-32, x86-64
OSCross-platform[4]
LicenseMIT[5][6]
Filename extensions.nim, .nims
Websitenim-lang.org
Influenced by
Ada, Modula-3, Lisp, C++, Object Pascal, Python, Oberon

DescriptionEdit

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 efficiently, as if they were builtin language facilities.[11] Iterators are supported and can be used as first class entities,[10] 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 can also be overloaded, and 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, which can be turned off altogether.[12] In 2014, Andrew Binstock (editor-in-chief of Dr. Dobb's Journal) said:

"Nimrod [former name] ... presents a most original design that straddles Pascal and Python and compiles to C code or JavaScript."[13]

Today, Nim compiles to C, C++, JavaScript, and Objective-C. The goal for Nim is to be as fast as C, as expressive as Python, and as extensible as Lisp.

HistoryEdit

Version Release date[14]
Old version, no longer maintained: 0.10.2 2014-12-29
Old version, no longer maintained: 0.11.2 2015-05-04
Old version, no longer maintained: 0.12.0 2015-10-27
Old version, no longer maintained: 0.13.0 2016-01-18
Old version, no longer maintained: 0.14.2 2016-06-09
Old version, no longer maintained: 0.15.2 2016-10-23
Old version, no longer maintained: 0.16.0 2017-01-08
Old version, no longer maintained: 0.17.2 2017-09-07
Old version, no longer maintained: 0.18.0 2018-03-01
Old version, no longer maintained: 0.19.6 2019-05-13
Old version, no longer maintained: 0.20.2 2019-06-17
Old version, no longer maintained: 1.0.0 2019-09-23
Old version, no longer maintained: 1.0.2 2019-10-23
Old version, no longer maintained: 1.0.4 2019-11-26
Old version, no longer maintained: 1.0.6 2020-01-24
Old version, no longer maintained: 1.2.0 2020-04-03
Old version, no longer maintained: 1.2.2 2020-06-17
Old version, no longer maintained: 1.2.4 2020-06-27
Old version, no longer maintained: 1.2.6 2020-07-30
Current stable version: 1.4.0 2020-10-16
Legend:
Old version
Older version, still maintained
Latest version
Latest preview version
Future release
For each 0.x branch, only the latest point release is listed.

Nim's initial development was started in 2005 by Andreas Rumpf. It was originally named Nimrod and the project was made public in 2008.[15] The first version of the Nim compiler was written in Pascal using the Free Pascal compiler.[16] In 2008, a version of the compiler written in Nim was released.[17] The compiler is free and open-source software and is being developed by a community of volunteers working with Andreas Rumpf.[18] The language was officially renamed from Nimrod to Nim with the release of version 0.10.2 in December 2014.[19] On the 23rd of September, 2019, version 1.0.0 of Nim was released signifying the maturation of the language and its toolchain.

Language designEdit

SyntaxEdit

The syntax of Nim resembles that of Python.[20] Code blocks and nesting statements are identified through use of white-space according to the offside-rule, and many keywords are identical to their Python equivalents, which are mostly English keywords, where other programming languages use punctuation. Even though Nim supports indentation based syntax like Python, it introduced flexibility whereby one could break statements with a comma or binary operator to the next line. Nim also supports user-defined operators.

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. Historically Nim was fully case-insensitive (meaning that capitalization and underscores of the identifiers were fully ignored).[21]

InfluenceEdit

In details, Nim is influenced by:

Uniform Function Call SyntaxEdit

Also, Nim supports Uniform Function Call Syntax (UFCS)[22] and identifier equality.

Example: Each line does the same call, just with different syntax:

echo "hello world"
echo("hello world")
"hello world".echo()
"hello world".echo
"hello".echo(" world")
"hello".echo " world"

Identifier EqualityEdit

Identifier in Nim are compared in a case insensitive manner except the first letter and underscores are ignored.

Example:

const useHttps = true
assert useHttps == useHttps
assert useHTTPS == useHttps
assert use_https == useHttps

StroppingEdit

Stropping is a method to mark explicitly a letter sequence for a special purpose. 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. 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.[23]

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`

CompilerEdit

The Nim compiler emits optimized C code by default and defers compiling to object code to an external C compiler[24] to leverage the existing optimizing and portability capabilities of the C compiler. Many C compilers are supported including Clang 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.[7] This allows writing applications for iOS and Android. There is also an unofficial LLVM backend allowing to use the Nim compiler in a stand-alone way.[25]

The Nim compiler is self-hosting, meaning it is written in the Nim language.[26] The Nim compiler supports cross compilation for other computers/operating systems from a different computer/OS. This is useful for compiling applications for embedded systems or uncommon computer architectures.

ToolsEdit

NimbleEdit

Nimble is the standard package manager used by Nim to package Nim modules,[27] it was initially developed by Dominik Picheta who is also a core Nim developer. Newer releases of Nim come with Nimble included.

Nimble packages are defined by .nimble files, which contain information about the package version, author, license, description, dependencies and more.[28] 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 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 command-line toolEdit

Because Nimble comes with the Nim compiler, it is possible to test the Nimble environment by running: nimble -v This command will reveal the version number, the compilation date and time and git hash of nimble. Nimble utilizes git, which must be available for Nimble to function well. The Nimble command-line is used as interface for installing, removing (uninstalling), and upgrading/patching module packages.[29]

c2nimEdit

c2nim is a transcompiler/transpiler that helps to generate new bindings by translating ANSI C code to Nim code.[30] The output is human-readable Nim code that is meant to be tweaked by hand after the translation process. Nim comes with c2nim included.

ChoosenimEdit

Choosenim was developed by Dominik Picheta, the author of the book Nim in Action, as a tool that enables a developer to have multiple versions of Nim compilers in their machine. Choosenim installs Nim from official downloads and sources, enabling easy switching between stable and development compilers.[31]

KochEdit

A maintenance script that is used to build Nim and also provide HTML documentation. Nim comes with Koch included.

NimgrepEdit

Nimgrep is a generic tool for manipulating text. It is used to search for regex or peg patterns or search the whole directory at once and also replace tasks. Nim comes with Nimgrep included.

NimsuggestEdit

Nimsuggest is a tool that helps any source code editor query a .nim source file and obtain useful information like definition of symbols or suggestions for completions. Nim comes with Nimsuggest included.

NiminstEdit

Niminst is a tool to generate an installer for a Nim program. Currently it can create an installer for Windows via Inno Setup as well as install/uninstall scripts for Linux, Mac and BSD. Nim comes with Niminst included.

NimprettyEdit

Nimpretty is a Nim source code beautifier, to format code according to the official style guide. Nim comes with Nimpretty included.

TestamentEdit

Testament is an advanced automatic Unittests runner for Nim tests, is used for the development of Nim itself, offers process isolation for your tests, it can generate statistics about test cases, supports multiple targets, simulated Dry-Runs, has logging, can generate HTML reports, skip tests from a file and more. Nim comes with Testament included.

DrNimEdit

DrNim is a tool that combines the Nim compiler Frontend with the Z3 proof engine in order to allow verify and validate software written in Nim. Nim comes with DrNim source code included, but requires compilation using Koch.

NimfixEdit

Nimfix is a tool that helps to convert old-style Nimrod code to Nim code.[32] Nimfix is currently beta-quality.[33] At this time, development and maintenance is mostly stalled.

pas2nimEdit

pas2nim is a tool to translate Object Pascal wrappers to Nim code.[34] 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 classes, and some other features are unsupported. At this time, development and maintenance is mostly stalled.

py2nimEdit

py2nim is a tool used for transpiling Python code into idiomatic Nim code.[35] Currently its development is stalled.

Standard LibraryEdit

Standard LibraryEdit

The Nim standard library can be classified into two groups known as pure and impure libraries.

Pure LibrariesEdit

Are a collection of modules written only in Nim without wrappers to access libraries written in other programming languages. The pure modules can be further classified into subgroups based on their tasks, which includes the following:

  • The Core
  • Collections and Algorithms
  • String Handling
  • Generic Operating System Services
  • Math Libraries
  • Internet Protocols
  • Parsers

Impure LibrariesEdit

The impure modules of Nim code which depend on external libraries that are written in other programming languages such as C.

LibrariesEdit

A Nim program can use any library which can be used in a C, C++ and JavaScript program. Language bindings exist for many libraries, including GTK+, Qt QML[36], wxWidgets[37], SDL2, Cairo, OpenGL, WinAPI, zlib, libzip, OpenSSL, Vulkan[38] and cURL.[39] Nim works with PostgreSQL, MySQL and SQLite databases. Nim can interface with the Lua[40], Julia[41], Rust[42], and Python[43] programming languages.

ExamplesEdit

Hello worldEdit

The "Hello, World!" program in Nim:

echo("Hello, world!")
# Procedures can be called with no parentheses
echo "Hello, World!"

Another version of making a "Hello World" is...

stdout.write("Hello, world!\n")

FactorialEdit

Program to calculate the factorial of a positive integer using the iterative approach:

import strutils

var n = 0
try:
  stdout.write "Input positive integer number: "
  n = stdin.readline.parseInt
except ValueError:
  raise newException(ValueError, "You must enter a positive number")

var fact = 1
for i in 2..n:
  fact = fact * i

echo fact

Using the module math from Nim's standard library:

import math

let i = fac(x)
echo i

Reversing a stringEdit

A simple demonstration showing many of Nim's features.

proc reverse(s: string): string =
  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, the compiler will attempt to use an items iterator, if one is defined for the type specified.

Graphical user interfaceEdit

Using GTK3 with gobject introspection through the gintro module:

import gintro/[gtk, glib, gobject, gio]

proc appActivate(app: Application) =
  let window = newApplicationWindow(app)
  window.title = "GTK3 application with gobject introspection"
  window.defaultSize = (400, 400)
  showAll(window)

proc main =
  let app = newApplication("org.gtk.example")
  connect(app, "activate", appActivate)
  discard run(app)

main()

This code requires the gintro module to work, which is not part of the standard library. To install the module gintro and many others you can use the tool nimble, which comes as part of nim. To install the gintro module with nimble you do the following:

nimble install gintro

Programming ParadigmsEdit

Object Oriented Programming (OOP)Edit

MetaprogrammingEdit

TemplateEdit

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

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

genType(Test, foo, int)

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

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

GenericEdit

Generics may be used in procedures, templates and macros. They are defined after the proc's name in square brackets, as seen below.

proc addThese[T](a, b: T): T =
  a + b

echo addThese(1, 2) # 3 (of int type)
echo addThese(uint8 1, uint8 2) # 3 (of uint8 type)

In addThese, T is the generic type, the compiler will accept any values for this function as long as both parameters and the return value are of the same type.

One can further clarify which types the procedure will accept by specifying a type class.[44]

proc addTheseNumbers[T: SomeNumber](a, b: T): T =
  a + b

addTheseNumberswill then only work for types contained in the SomeNumbersum type.

MacrosEdit

Macros can literally rewrite parts of the code before it's compiled. Nim macros are powerful and can do many operations on the abstract syntax tree.

Here's a simple example, that creates a macro called twice:

import macros

macro twice(arg: untyped): untyped =
  result = quote do:
    `arg`
    `arg`

twice echo "Hello world!"

The twice macro in this example takes the echo statement in the form of an abstract syntax tree as input. In this example we decided to return this syntax tree without any manipulations applied to it. But we do it twice, hence the name of the macro. The end result is that the code gets rewritten by the macro to look like the following code at compile time:

echo "Hello world!"
echo "Hello world!"

Foreign Function Interface (FFI)Edit

Nim's FFI is used to call functions written in the other programming languages that it can compile to. This means that libraries written in C, C++, Objective-C and JavaScript can be used in the Nim source code. One should be aware that both JavaScript and C, C++, or Objective-C libraries cannot be combined in the same program, as they are not as compatible with JavaScript as they are with each other. Both C++ and Objective-C are based on and compatible with C, but JavaScript is incompatible, as a dynamic, client-side web-based language.[45]

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

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

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

In this code the printf function is imported into Nim and then used.

ParallelismEdit

To activate threading support in Nim, a program should be compiled with --threads:on command line argument. Each thread has a separate garbage collected heap and sharing of memory is restricted, which helps with efficiency and stops race conditions by the threads.

import locks

var
  thr: array[0..4, Thread[tuple[a,b: int]]]
  L: Lock

proc threadFunc(interval: tuple[a,b: int]) {.thread.} =
  for i in interval.a..interval.b:
    acquire(L) # lock stdout
    echo i
    release(L)

initLock(L)

for i in 0..high(thr):
  createThread(thr[i], threadFunc, (i*10, i*10+5))
joinThreads(thr)

Nim also has a channels module that simplifies passing data between threads.

import os

type
  CalculationTask = object
    id*: int
    data*: int

  CalculationResult = object
    id*: int
    result*: int

var task_queue: Channel[CalculationTask]
var result_queue: Channel[CalculationResult]

proc workerFunc() {.thread.} =
  result_queue.open()

  while true:
    var task = task_queue.recv()
    result_queue.send(CalculationResult(id: task.id, result: task.data * 2))

var workerThread: Thread[void]
createThread(workerThread, workerFunc)

task_queue.open()
task_queue.send(CalculationTask(id: 1, data: 13))
task_queue.send(CalculationTask(id: 2, data: 37))

while true:
  echo "got result: ", repr(result_queue.recv())

ConcurrencyEdit

Nim supports asynchronous IO via the asyncdispatch module, which adds async/await syntax via the macro system. An example of an asynchronous http server:

import asynchttpserver, asyncdispatch

var server = newAsyncHttpServer()
proc cb(req: Request) {.async.} =
  await req.respond(Http200, "Hello World")

waitFor server.serve(Port(8080), cb)

Nim CommunityEdit

The project uses a Git repository, bug tracker and wiki hosted by GitHub, and a self-developed and self-hosted forum.[46][47] A presentation on Nim was given at the O'Reilly Open Source Convention (OSCON) in 2015.[48][49][50] At FOSDEM 2020 four speakers represented Nim, with the creator of the language, Andreas Rumpf, being one of them.[51]

See alsoEdit

ReferencesEdit

  1. ^ "Nim Releases". Retrieved 2020-10-16.
  2. ^ "Nim by example". GitHub. Retrieved 2014-07-20.
  3. ^ Караджов, Захари; Станимиров, Борислав (2014). Метапрограмиране с Nimrod. VarnaConf (in Bulgarian). Retrieved 2014-07-27.
  4. ^ "Install Nim". Retrieved 2018-10-12.
  5. ^ a b "FAQ". Nim-lang.org. Retrieved 2015-03-27.
  6. ^ "copying.txt". 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". Nim-lang.org. Retrieved 2014-07-20.
  9. ^ Kehrer, Aaron (akehrer). "Nim Syntax". GitHub. Retrieved 2015-01-05.
  10. ^ a b c "Nim Manual". Nim-lang.org. 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-lang.org. Retrieved 2018-01-10.
  13. ^ Binstock, Andrew (2014-01-07). "The Rise And Fall of Languages in 2013". Dr. Dobb's Journal. Retrieved 2018-10-08.
  14. ^ "Nim Releases". Nim Project. Retrieved 2020-01-26.
  15. ^ Picheta, Dominik (2017). "1.1 What is Nim?". Nim in Action. Manning Publications. ISBN 9781617293436.
  16. ^ "Nim Pascal Sources". GitHub. Retrieved 2013-04-05.
  17. ^ "News". Nim-lang.org. Archived from the original on 2016-06-26. Retrieved 2016-06-11.
  18. ^ "Contributors". GitHub. Retrieved 2013-04-05.
  19. ^ Picheta, Dominik (2014-12-29). "Version 0.10.2 released". Nim-lang.org. Retrieved 2018-10-17.
  20. ^ Yegulalp, Serdar (2017-01-16). "Nim language draws from best of Python, Rust, Go, and Lisp". InfoWorld.
  21. ^ "Nim Manual". nim-lang.org. Retrieved 2020-07-21.
  22. ^ "Nim Manual: Method call syntax". Retrieved 2018-10-12.
  23. ^ Picheta, Dominik (dom96); Wetherfordshire, Billingsly (fowlmouth); Felsing, Dennis (def-); Raaf, Hans (oderwat); Dunn, Christopher (cdunn2001); wizzardx (2017-10-25). "Tips and tricks". GitHub. Retrieved 2018-10-17.
  24. ^ Rumpf, Andreas (2014-01-15). Nimrod: A New Approach to Metaprogramming. InfoQ. Event occurs at 2:23. Retrieved 2014-07-20.
  25. ^ Sieka, Jacek (2020-07-18), arnetheduck/nlvm, retrieved 2020-07-21
  26. ^ Rumpf, Andreas (2018-10-12). "Nim Compiling". GitHub. Retrieved 2018-10-17.
  27. ^ "Nimble". GitHub. Retrieved 2018-10-12.
  28. ^ Picheta, Dominik (2017). Nim in Action. Manning Publications. p. 132. ISBN 9781617293436.
  29. ^ Picheta, Dominik (2017). Nim in Action. Manning Publications. pp. 130–131. ISBN 9781617293436.
  30. ^ "c2nim". GitHub. Retrieved 2018-10-12.
  31. ^ "choosenim". GitHub. Retrieved 2018-10-12.
  32. ^ "nimfix.nim". GitHub. Retrieved 2018-10-12.
  33. ^ "nimfix.nim".
  34. ^ "pas2nim". GitHub. Retrieved 2018-10-12.
  35. ^ "py2nim". GitHub. Retrieved 2018-10-12.
  36. ^ "NimQml". GitHub.
  37. ^ "WxNim". GitHub.
  38. ^ "Vulkanim". GitHub.
  39. ^ "Nim Standard Library". Nim documentation. Archived from the original on 2015-04-06. Retrieved 2015-04-04.
  40. ^ Lim, Andri (jangko) (2018-10-17). "nimLUA". GitHub. Retrieved 2018-10-17.
  41. ^ "Nimjl". GitHub.
  42. ^ "Nbindgen". GitHub.
  43. ^ Glukhov, Yuriy (2020-07-20), yglukhov/nimpy, retrieved 2020-07-21
  44. ^ "Nim Manual". nim-lang.org. Retrieved 2020-07-21.
  45. ^ Picheta, Dominik (2017). Nim in Action. Manning Publishing. p. 226. ISBN 9781617293436.
  46. ^ "Primary source code repository and bug tracker". GitHub. Retrieved 2015-05-04.
  47. ^ "Nim Forum". nim-lang.org. Retrieved 2015-05-04.
  48. ^ "Nim at OSCON 2015". O'Reilly Open Source Convention (OSCON). O'Reilly Media. 2015-07-20. Retrieved 2018-10-17.
  49. ^ Rumpf, Andreas; Swartz, Jason; Harrison, Matt. "Essential Languages: Nim, Scala, Python". O’Reilly. O'Reilly Media. Retrieved 2018-10-17.
  50. ^ Rumpf, Andreas (2015-10-26). OSCON 2015 – Andreas Rumpf – Nim: An Overview. YouTube (Video). Retrieved 2018-10-12.
  51. ^ "Events". fosdem.org. Retrieved 2020-02-17.

External linksEdit