Nim (programming language)

Nim
Paradigm multi-paradigm: compiled, concurrent, procedural, imperative, object-oriented
Designed by Andreas Rumpf
First appeared 2008 (2008)
Preview release
0.19.0[1] / 26 September 2018 (2018-09-26)
Typing discipline static,[2] strong,[3] inferred, structural
Platform IA-32, x86-64
OS Cross-platform[4]
License MIT[5][6]
Filename extensions .nim
Website nim-lang.org
Influenced by
Ada, Modula-3, Lisp, C++, Object Pascal, Python, Oberon

Nim (formerly named Nimrod) is an imperative, multi-paradigm, 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 and compiling to JavaScript, C, and C++.

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.[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

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

Language design

The syntax of Nim is similar to Python.[18]

In details, it is influenced by:

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

Compiler

The Nim compiler emits optimized C code and defers compiling to an external compiler[21] 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.[22]

Tools

Nimble

Nimble is the package manager used by Nim to package Nim modules.[23] 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.[24] 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.[25]

Nimfix

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

pas2nim

pas2nim is a tool to translate Object Pascal wrappers to Nim code.[28] 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, the project is largely unmaintained.

py2nim

py2nim is a tool used for transpiling Python code into idiomatic Nim code.[29] The project is in active development, 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.[30] Nim works with PostgreSQL, MySQL and SQLite databases. Nim can interface with the Lua[31] and Python interpreter.[32]

Examples

The following code examples are valid as of Nim 0.17.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 =
  result = "" # implicit result variable
  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.[33]

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 existing 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.[34][35] A presentation at O'Reilly Open Source Convention (OSCON) in 2015 took place.[36] O'Reilly Community: Essential Languages: Nim, Scala, Python.[37][38]

See also

References

  1. "Version 0.19.0 released". 2018-09-26. Retrieved 2018-10-12.
  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. 1 2 "FAQ". Nim-lang.org. Retrieved 2015-03-27.
  6. "copying.txt". Nim. GitHub. Retrieved 2015-03-27.
  7. 1 2 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. "Nim Syntax". akehrer. Retrieved 2015-01-05.
  10. 1 2 3 "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 documentation. 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 Pascal Sources". Nim. GitHub. Retrieved 2013-04-05.
  15. "News". Nim-lang.org. Archived from the original on 2016-06-26. Retrieved 2016-06-11.
  16. "Contributors". Nim. GitHub. Retrieved 2013-04-05.
  17. Picheta, Dominik (2014-12-29). "Version 0.10.2 released". Nim-lang.org.
  18. Yegulalp, Serdar (2017-01-16). "Nim language draws from best of Python, Rust, Go, and Lisp". InfoWorld.
  19. "Nim Manual: Method call syntax". Retrieved 2018-10-12.
  20. "Nim Manual: Identifier equality". Retrieved 2018-10-12.
  21. Rumpf, Andreas (2014-01-15). Nimrod: A New Approach to Metaprogramming. InfoQ. Event occurs at 2:23. Retrieved 2014-07-20.
  22. "Nim Compiling". Retrieved 2018-10-12.
  23. "Nimble". Retrieved 2018-10-12.
  24. "c2nim". Retrieved 2018-10-12.
  25. "choosenim". Retrieved 2018-10-12.
  26. "nimfix.nim". Retrieved 2018-10-12.
  27. "nimfix.nim".
  28. "pas2nim". Retrieved 2018-10-12.
  29. "py2nim". Retrieved 2018-10-12.
  30. "Nim Standard Library". Nim documentation. Archived from the original on 2015-04-06. Retrieved 2015-04-04.
  31. https://github.com/jangko/nimLUA
  32. https://akehrer.github.io/posts/connecting-nim-to-python/
  33. https://github.com/nim-lang/Nim/wiki/Tips-and-tricks
  34. "Primary source code repository and bug tracker". GitHub. Retrieved 2015-05-04.
  35. "Nim Forum". nim-lang.org. Retrieved 2015-05-04.
  36. "Nim at OSCON 2015". OSCON. 2015-07-20. Retrieved 2015-05-04.
  37. "Essential Languages: Nim, Scala, Python".
  38. Rumpf, Andreas (2015-10-26). OSCON 2015 – Andreas Rumpf – Nim: An Overview (Video). YouTube. Retrieved 2018-10-12.
This article is issued from Wikipedia. The text is licensed under Creative Commons - Attribution - Sharealike. Additional terms may apply for the media files.