Nim (programming language)

This is an old revision of this page, as edited by BG19bot (talk | contribs) at 09:10, 18 February 2015 (WP:CHECKWIKI error fix for #61. Punctuation goes before References. Do general fixes if a problem exists. - using AWB (10822)). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.

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,[8] functional, message passing,[9] procedural, and object-oriented programming styles.

Nim
Paradigmcompiled, concurrent, procedural, imperative, object-oriented
Designed byAndreas Rumpf
First appeared2008; 17 years ago (2008)
Preview release
0.10.2[1] / 2014-12-29
Typing disciplinestatic,[2] strong,[3] inferred, structural
OSLinux, Mac OS X, Windows, FreeBSD, NetBSD
LicenseMIT License[4][5]
Websitenim-lang.org
Influenced by
Ada, Modula 3, Lisp, C++, Object Pascal, Python

The Nim compiler was initially written in Pascal,[10] 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.[11] The compiler generates optimized C code and defers compilation to an external compiler[12] (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[13]

Description

Nim's syntax is an unusual blend between Python and Pascal.[14] The language shares many syntactical similarities to Python[15] (for example the if and for statements are practically identical), Nim however differs greatly when it comes to semantics: for a start Nim is statically typed.

Nim supports compile-time metaprogramming features such as AST macros and term rewriting macros,[16] the combination of these features is largely unique to Nim.[17] 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[16] 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.[16]

Examples

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

echo "Hello World!"

Reversing a string

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)

This example shows many of Nim's 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(Test, foo, int)

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

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

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

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

Existing C code can directly be used in Nim. In this code the well known printf function is imported into Nim and subsequently used.[18]

References

  1. ^ a b "News". Official website. Retrieved 2015-01-02.
  2. ^ "Nim by example". Github. Retrieved 2014-07-20.
  3. ^ Караджов, Захари; Станимиров, Борислав (2014). Метапрограмиране с Nimrod. VarnaConf (in Russian). Retrieved 2014-07-27. {{cite conference}}: External link in |conferenceurl= (help); Unknown parameter |conferenceurl= ignored (|conference-url= suggested) (help)
  4. ^ "FAQ". Official website. Retrieved 2014-07-20.
  5. ^ "copying.txt". Nim. Github. Retrieved 2014-07-20.
  6. ^ a b Rumpf, Andreas (2014-02-11). "Nimrod: A new systems programming language". Dr. Dobb's Journal. Retrieved 2014-07-20.
  7. ^ "The Nim Programming Language". Official website. Retrieved 2014-07-20.
  8. ^ "Nim Programming Language Gaining Traction". Slashdot anonymous poster. Retrieved 15 Feb 2014.
  9. ^ "FAQ". Official website. Retrieved 2013-04-05.
  10. ^ "Nim Pascal Sources". Nim. Github. Retrieved 2013-04-05.
  11. ^ "Contributors". Nim. Github. Retrieved 2013-04-05.
  12. ^ Rumpf, Andreas (2014-01-15). Nimrod: A New Approach to Metaprogramming. InfoQ. Event occurs at 2:23. Retrieved 2014-07-20.
  13. ^ Hankiewicz, Grzegorz Adam (2014-03-10). "Nimrod for cross platform software". Rants from the Ballmer Peak. Github. Retrieved 2014-07-20.
  14. ^ Binstock, Andrew (2014-01-07). "The Rise And Fall of Languages in 2013". Dr. Dobb's.
  15. ^ Picheta, Dominik (2013-10-27). "About Nimrod's features". Blog. Retrieved 2014-07-20.
  16. ^ a b c "Nim Manual". Official website. Retrieved 2014-07-20.
  17. ^ Araq (2012-09-10). "Term rewriting macros". Nim Forum. Retrieved 2014-07-20.
  18. ^ "What is special about Nim?". HookRace. 2015-01-01. Retrieved 2015-02-17.