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, functional, message passing,[8] procedural, and object-oriented programming styles.
Nim | |
---|---|
Paradigm | compiled, concurrent, procedural, imperative, object-oriented |
Designed by | Andreas Rumpf |
First appeared | 2008 |
Preview release | 0.10.2[1]
/ 2014-12-29 |
Typing discipline | static[2], strong[3], inferred, structural |
OS | Linux, Mac OS X, Windows, FreeBSD, NetBSD |
License | MIT License[4][5] |
Website | www |
Influenced by | |
Ada, Modula 3, Lisp, C++, Object Pascal, Python |
The Nim compiler was initially written in Pascal,[9] 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.[10] The compiler generates optimized C code and defers compilation to an external compiler[11] (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[12]
Description
Nim's syntax is an unusual blend between Python and Pascal[13]. The language shares many syntactical similarities to Python[14] (for example the if
and for
statements are practically identical), Nim however differs greatly when it comes to semantics: for a start Nimr is statically typed.
Nim supports compile-time metaprogramming features such as AST macros and term rewriting macros,[15] the combination of these features is largely unique to Nimrod.[16] 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. Iterators are supported and can be used as first class entities[15] 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.[15]
Examples
The following code examples are valid as of Nim 0.9.4. Syntax and semantics may change in subsequent versions.
echo "Hello World!"
Reversing a string
proc reverse(s: string): string =
result = ""
for i in countdown(high(s), 0):
result.add s[i]
var str1 = "Reverse This!"
echo "Reversed: ", reverse(str1)
This example shows many Nim 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(TTest, foo, int)
var x = TTest(foo: 4566)
echo(x.foo) # 4566
This an example of metaprogramming in Nim using its template facilities. The GenType
is invoked at compile-time and a TTest
type is created.
References
- ^ a b "News". Official website. Retrieved 2015-01-02.
- ^ "Nimrod by example". Github. Retrieved 2014-07-20.
- ^ Караджов, Захари; Станимиров, Борислав (2014). Метапрограмиране с Nimrod. VarnaConf (in Russian). Retrieved 2014-07-27.
{{cite conference}}
: External link in
(help); Unknown parameter|conferenceurl=
|conferenceurl=
ignored (|conference-url=
suggested) (help) - ^ "FAQ". Official website. Retrieved 2014-07-20.
- ^ "copying.txt". Nimrod. Github. Retrieved 2014-07-20.
- ^ a b c Rumpf, Andreas (2014-02-11). "Nimrod: A new systems programming language". Dr. Dobb's Journal. Retrieved 2014-07-20.
- ^ "The Nimrod Programming Language". Official website. Retrieved 2014-07-20.
- ^ "FAQ". Official website. Retrieved 2013-04-05.
- ^ "Nimrod pascal sources". Nimrod. Github. Retrieved 2013-04-05.
- ^ "Contributors". Nimrod. 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.
- ^ Binstock, Andrew (2014-01-07). "The Rise And Fall of Languages in 2013". Dr. Dobb's.
- ^ Picheta, Dominik (2013-10-27). "About Nimrod's features". Blog. Retrieved 2014-07-20.
- ^ a b c "Nimrod Manual". Official website. Retrieved 2014-07-20.
- ^ Araq (2012-09-10). "Term rewriting macros". Nimrod Forum. Retrieved 2014-07-20.
External links
- Official website
- Nim Language Wiki
- Nim Forum
- Primary source code repository and bug tracker
- Nim code examples at Rosetta Code
Category:Systems programming languages Category:Concurrent programming languages Category:Statically typed programming languages Category:Multi-paradigm programming languages Category:Functional languages Category:Procedural programming languages