Nim (programming language)

Nim
The logo for Nim
Paradigmmulti-paradigm: compiled, concurrent, Procedural, Imperative, Functional, Object-oriented,
Designed byAndreas Rumpf
First appeared2008; 11 years ago (2008)
Preview release
0.19.4[1] / 1 February 2019; 2 months ago (2019-02-01)
Typing disciplinestatic,[2] strong,[3] inferred, structural
PlatformIA-32, x86-64
OSCross-platform[4]
LicenseMIT[5][6]
Filename extensions.nim
Websitenim-lang.org
Influenced by
Ada, Modula-3, Lisp, C++, Object Pascal, Python, Oberon

Nim (formerly named Nimrod) 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++ and compiling to C, C++, Objective-C and Javascript.

Description

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 with an efficiency as if they were builtin language facilities.[11] Iterators are supported and can be used as first class entities[10] 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.[10] Nim includes tunable automatic garbage collection based on deferred reference counting with cycle detection, which can be swiched off altogether if not wanted.[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++ too.

History

Version Release date[14]
Old version, no longer supported: 0.10.2 2014-12-29
Old version, no longer supported: 0.11.2 2015-05-04
Old version, no longer supported: 0.12.0 2015-10-27
Old version, no longer supported: 0.13.0 2016-01-18
Old version, no longer supported: 0.14.2 2016-06-09
Old version, no longer supported: 0.15.2 2016-10-23
Old version, no longer supported: 0.16.0 2017-01-08
Old version, no longer supported: 0.17.2 2017-09-77
Old version, no longer supported: 0.18.0 2018-03-01
Current stable version: 0.19.4 2019-02-01
Legend:
Old version
Older version, still supported
Latest version
Latest preview version
Future release
For each 0.x branch, only the latest point release is listed.

Nim's initial development began in 2005 by Andreas Rumpf and the project was released to Github site in 2006. The first version of the Nim compiler was written in Pascal using the Free Pascal compiler.[15] In 2008, a version of the compiler written in Nim was released.[16] The compiler is free and open-source software and is being developed by a group of volunteers(Nim Community) working with Andreas Rumpf.[17] The language was officially renamed from Nimrod to Nim with the release of version 0.10.2 in December 2014.[18]

Language design

Syntax

The syntax of Nim is similar to Python.[19] Code blocks are identified through use of white-space according to the offside-rule and many keywords are identical to their Python equivalents. Nim also supports used 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.[20]

Others

In details, it is influenced by:

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

Compiler

The Nim compiler emits optimized C code and defers compiling to an external compiler[23] to leverage their optimizing and portability abilities. Many compilers are supported including Clang and GNU Compiler Collection (GCC). The 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.

The Nim compiler is self-hosting, meaning it is written in the Nim language.[24]

Tools

Nimble

Nimble is the package manager used by Nim to package Nim modules.[25] 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 JavaScript Object Notation (JSON) file which is freely accessible in the nim-lang/packages repository. This JSON file provides nimble with the needed Git URL to clone the package and install it.

c2nim

c2nim helps to generate new bindings by translating ANSI C code to Nim code.[26] The output is human-readable Nim code that is meant to be tweaked by hand after the translation process.

Choosenim

Choosenim installs Nim from official downloads and sources, enabling easy switching between stable and development compilers.[27]

Nimfix

Nimfix is a tool that helps to convert old-style Nimrod code to Nim code.[28] Nimfix is currently beta-quality.[29]

pas2nim

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

py2nim

py2nim is a tool used for transpiling Python code into idiomatic Nim code.[31] Development is active, with plans to extend the amount of Python code that can be fully translated into Nim.

Libraries

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

Examples

The following code examples are valid as of Nim 0.19.0. Because Nim has not had its 1.0 release, syntax and semantics may change in later versions.

Hello world

The "Hello, World!" program in Nim:

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

Reversing a string

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 then the compiler will attempt to use an items iterator if one is defined for the type that was specified in the for loop.

Stropping

Stropping is a method to mark explicitly a letter sequence as having a special property. 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 in that case. 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 when inside backticks.[35]

var `type`: int

Metaprogramming

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.

Wrapping C functions

The following program demonstrates the ease with which extant 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 well known printf function is imported into Nim and then used.

Community

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

See also

References

  1. ^ "Version 0.19.4 released". 2019-02-01. Retrieved 2019-01-01.
  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 Blog". Nim Project. Retrieved 2019-04-07.
  15. ^ "Nim Pascal Sources". GitHub. Retrieved 2013-04-05.
  16. ^ "News". Nim-lang.org. Archived from the original on 2016-06-26. Retrieved 2016-06-11.
  17. ^ "Contributors". GitHub. Retrieved 2013-04-05.
  18. ^ Picheta, Dominik (2014-12-29). "Version 0.10.2 released". Nim-lang.org. Retrieved 2018-10-17.
  19. ^ Yegulalp, Serdar (2017-01-16). "Nim language draws from best of Python, Rust, Go, and Lisp". InfoWorld.
  20. ^ "Nim Manual: Lexical Analysis". nim-lang.github.io. Retrieved 2019-04-27.
  21. ^ "Nim Manual: Method call syntax". Retrieved 2018-10-12.
  22. ^ "Nim Manual: Identifier equality". Retrieved 2018-10-12.
  23. ^ Rumpf, Andreas (2014-01-15). Nimrod: A New Approach to Metaprogramming. InfoQ. Event occurs at 2:23. Retrieved 2014-07-20.
  24. ^ Rumpf, Andreas (2018-10-12). "Nim Compiling". GitHub. Retrieved 2018-10-17.
  25. ^ "Nimble". GitHub. Retrieved 2018-10-12.
  26. ^ "c2nim". GitHub. Retrieved 2018-10-12.
  27. ^ "choosenim". GitHub. Retrieved 2018-10-12.
  28. ^ "nimfix.nim". GitHub. Retrieved 2018-10-12.
  29. ^ "nimfix.nim".
  30. ^ "pas2nim". GitHub. Retrieved 2018-10-12.
  31. ^ "py2nim". GitHub. Retrieved 2018-10-12.
  32. ^ "Nim Standard Library". Nim documentation. Archived from the original on 2015-04-06. Retrieved 2015-04-04.
  33. ^ Lim, Andri (jangko) (2018-10-17). "nimLUA". GitHub. Retrieved 2018-10-17.
  34. ^ Kehrer, Aaron (akehrer) (2015-01-24). "Connecting Nim to Python". GitHub. Retrieved 2018-10-17.
  35. ^ 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.
  36. ^ "Primary source code repository and bug tracker". GitHub. Retrieved 2015-05-04.
  37. ^ "Nim Forum". nim-lang.org. Retrieved 2015-05-04.
  38. ^ "Nim at OSCON 2015". O'Reilly Open Source Convention (OSCON). O'Reilly Media. 2015-07-20. Retrieved 2018-10-17.
  39. ^ Rumpf, Andreas; Swartz, Jason; Harrison, Matt. "Essential Languages: Nim, Scala, Python". O’Reilly. O'Reilly Media. Retrieved 2018-10-17.
  40. ^ Rumpf, Andreas (2015-10-26). OSCON 2015 – Andreas Rumpf – Nim: An Overview. YouTube (Video). Retrieved 2018-10-12.

External links