Nim (programming language)
Paradigm | multi-paradigm: compiled, concurrent, procedural, imperative, object-oriented |
---|---|
Designed by | Andreas Rumpf |
First appeared | 2008 |
Preview release | 0.15.2[1]
/ October 23, 2016 |
Typing discipline | static,[2] strong,[3] inferred, structural |
OS | Cross-platform[citation needed] |
License | MIT[4][5] |
Filename extensions | .nim |
Website | nim-lang |
Influenced by | |
Ada, Modula-3, Lisp, C++, Object Pascal, Python, Oberon |
Nim (formerly named Nimrod) is an imperative, multi-paradigm, compiled programming language[6] designed and developed by Andreas Rumpf. It is designed to be "efficient, expressive, and elegant",[7] supporting metaprogramming, functional, message passing,[4] 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.[8]
Description
Nim is statically typed, with a simple syntax.[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 would have been 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 automatic garbage collection based on deferred reference counting with cycle detection.[12][13][self-published source?] 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."[14]
History
The initial development of Nim began in 2005 by Andreas Rumpf. The Nim compiler was written in Pascal.[15] In 2008, a version of the compiler written in Nim was released.[16] The compiler is open source and is being developed by a group of volunteers in addition to Andreas Rumpf.[17]
Language design
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
Compiler
The compiler emits optimized C code and defers compiling to an external compiler[18] (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.[6] This allows writing applications for iOS and Android.[19][20]
Package system
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.
Tools
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.[21] 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.[22]
Hello world
The Hello world program in Nim:
echo "Hello World!"
Reversing a string
A simple demonstration showing many of Nim's features.
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)
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.
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
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.
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 subsequently used.[23]
Community
The language has a bug tracker with wiki hosted by GitHub and a forum.[24][25] A presentation at O'Reilly Open Source Convention (OSCON) in 2015 took place.[26] O'Reilly Community: Essential Languages: Nim, Scala, Python.[27][28]
See also
- D (programming language)
- UFCS, a feature supported by Nim
References
- ^ Picheta, Dominik; Rumpf, Andreas (2016-10-23). "Version 0.15.2 released". Nim. Archived from the original on 2016-10-01. Retrieved 2016-10-01.
{{cite web}}
: Unknown parameter|dead-url=
ignored (|url-status=
suggested) (help) - ^ "Nim by example". GitHub. Retrieved 2014-07-20.
- ^ Караджов, Захари; Станимиров, Борислав (2014). Метапрограмиране с Nimrod. VarnaConf (in Bulgarian). Retrieved 2014-07-27.
{{cite conference}}
: External link in
(help); Unknown parameter|conferenceurl=
|conferenceurl=
ignored (|conference-url=
suggested) (help) - ^ a b "FAQ". Official website. Retrieved 2015-03-27.
- ^ "copying.txt". Nim. GitHub. Retrieved 2015-03-27.
- ^ a b Rumpf, Andreas (2014-02-11). "Nimrod: A new systems programming language". Dr. Dobb's Journal. Retrieved 2014-07-20.
- ^ "The Nim Programming Language". Official website. Retrieved 2014-07-20.
- ^ "What is special about Nim?". Hookrace blog. Retrieved 2015-03-27.
- ^ "Nim Syntax". akehrer. Retrieved 2015-03-27.
- ^ a b c "Nim Manual". Official website. Retrieved 2014-07-20.
- ^ "Strangeloop Nim presentation". Retrieved 2015-04-30.
- ^ "Nim's Garbage Collector". Nim documentation. Retrieved 2015-04-03.
- ^ "A Quick Comparison of Nim vs. Rust".
- ^ The Rise And Fall of Languages in 2013 By Andrew Binstock, January 07, 2014 Dr. Dobb's
- ^ "Nim Pascal Sources". Nim. GitHub. Retrieved 2013-04-05.
- ^ "News". Official website. Retrieved 2016-06-11.
- ^ "Contributors". Nim. GitHub. Retrieved 2013-04-05.
- ^ Rumpf, Andreas (2014-01-15). Nimrod: A New Approach to Metaprogramming. InfoQ. Event occurs at 2:23. Retrieved 2014-07-20.
- ^ Hankiewicz, Grzegorz Adam (2014-03-10). "Nimrod for cross platform software". Rants from the Ballmer Peak. GitHub. Retrieved 2014-07-20.
- ^ "Nimrod-on-android failure". 2012-07-28. Retrieved 2015-02-28.
- ^ "Nim Standard Library". Nim documentation. Retrieved 2015-04-04.
- ^ Nim code examples at Rosetta Code
- ^ "What is special about Nim?". HookRace. 2015-01-01. Retrieved 2015-02-17.
- ^ "Primary source code repository and bug tracker". GitHub. Retrieved 2015-05-04.
- ^ Nim "Forum". nim-lang.org. Retrieved 2015-05-04.
{{cite web}}
: Check|url=
value (help) - ^ "Nim at OSCON 2015". OSCON. 2015-07-20. Retrieved 2015-05-04.
- ^ "Essential Languages: Nim, Scala, Python".
- ^ Presentation of Nim by Andreas Rumpf on OSCON 2015 on YouTube