Jump to content

Nim (programming language): Difference between revisions

From Wikipedia, the free encyclopedia
Content deleted Content added
Itsmeront (talk | contribs)
No edit summary
Itsmeront (talk | contribs)
Line 25: Line 25:
== Description ==
== Description ==


Nim is a statically typed programming language with a simple syntax.<ref name="Nim Syntax">{{cite web|url=https://akehrer.github.io/nim/2015/01/05/getting-started-with-nim.html|title=Nim Syntax|work=akehrer|accessdate=2015-03-27}}</ref> Nim supports compile-time metaprogramming features such as syntactic macros and [[Macro_(computer_science)#Syntactic_macros|term rewriting macros]].<ref name="manual">{{cite web|url=http://nim-lang.org/manual.html|title=Nim Manual|work=Official website|accessdate=2014-07-20}}</ref> 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. <ref name = "strangeloop">{{cite web|url=https://thestrangeloop.com/sessions/nimrod-a-new-approach-to-meta-programming|title=Strangeloop Nim presentation|accessdate=2015-04-30}}</ref>Iterators are supported and can be used as first class entities<ref name="manual"/> 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.<ref name="manual"/> Nim includes [[Garbage collection (computer science)|automatic garbage collection]] based on deferred [[reference counting]] with cycle detection.<ref name="gc">{{cite web|url=http://nim-lang.org/gc.html|title=Nim's Garbage Collector|work=Nim documentation|accessdate=2015-04-03}}</ref></ref>{{cite web|title=A Quick Comparison of Nim vs. Rust|url=http://arthurtw.github.io/2015/01/12/quick-comparison-nim-vs-rust.html}}</ref>
Nim is a statically typed programming language with a simple syntax.<ref name="Nim Syntax">{{cite web|url=https://akehrer.github.io/nim/2015/01/05/getting-started-with-nim.html|title=Nim Syntax|work=akehrer|accessdate=2015-03-27}}</ref> Nim supports compile-time metaprogramming features such as syntactic macros and [[Macro_(computer_science)#Syntactic_macros|term rewriting macros]].<ref name="manual">{{cite web|url=http://nim-lang.org/manual.html|title=Nim Manual|work=Official website|accessdate=2014-07-20}}</ref> 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. <ref name = "strangeloop">{{cite web|url=https://thestrangeloop.com/sessions/nimrod-a-new-approach-to-meta-programming|title=Strangeloop Nim presentation|accessdate=2015-04-30}}</ref>Iterators are supported and can be used as first class entities<ref name="manual"/> 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.<ref name="manual"/> Nim includes [[Garbage collection (computer science)|automatic garbage collection]] based on deferred [[reference counting]] with cycle detection.<ref name="gc">{{cite web|url=http://nim-lang.org/gc.html|title=Nim's Garbage Collector|work=Nim documentation|accessdate=2015-04-03}}</ref><ref>{{cite web|title=A Quick Comparison of Nim vs. Rust|url=http://arthurtw.github.io/2015/01/12/quick-comparison-nim-vs-rust.html}}</ref>


== Examples ==
== Examples ==

Revision as of 19:00, 30 May 2015

Nim
Paradigmcompiled, concurrent, procedural, imperative, object-oriented
Designed byAndreas Rumpf
First appeared2008; 16 years ago (2008)
Preview release
0.11.2[1] / 2015-05-04
Typing disciplinestatic,[2] strong,[3] inferred, structural
OSWindows, OS X, Linux, FreeBSD, NetBSD
LicenseMIT License[4][5]
Websitenim-lang.org
Influenced by
Ada, Modula-3, Lisp, C++, Object Pascal, Python, Oberon

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,[4] procedural, and object-oriented programming styles by providing several features such as compile time code generation, algebraic data types, an elegant FFI with C and compilation to Javascript.[8]

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][13]

Description

Nim is a statically typed programming language with a simple syntax.[14] Nim supports compile-time metaprogramming features such as syntactic macros and term rewriting macros.[15] 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. [16]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] Nim includes automatic garbage collection based on deferred reference counting with cycle detection.[17][18]

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

Libraries

A Nim program can use any library which can be used in a C program. There are existing bindings for many libraries, for example GTK+2, SDL2, Cairo, OpenGL, WinAPI, zlib, libzip, OpenSSL and cURL.[20] Nim works with PostgreSQL, MySQL and SQLite databases. Nim can interface with the Lua and Python interpreter. The tool c2nim helps to generate new bindings from C code.

Community

The language has a bug tracker with wiki hosted by GitHub and a forum.[21][22] A presentation at OSCON in 2015 is scheduled.[23]

References

  1. ^ a b "News". Official website. Retrieved 2015-05-04.
  2. ^ "Nim by example". GitHub. Retrieved 2014-07-20.
  3. ^ Караджов, Захари; Станимиров, Борислав (2014). Метапрограмиране с Nimrod. VarnaConf (in Bulgarian). Retrieved 2014-07-27. {{cite conference}}: External link in |conferenceurl= (help); Unknown parameter |conferenceurl= ignored (|conference-url= suggested) (help)
  4. ^ a b "FAQ". Official website. Retrieved 2015-03-27.
  5. ^ "copying.txt". Nim. GitHub. Retrieved 2015-03-27.
  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. ^ "What's so special about Nim?". Hookrace blog. Retrieved 2015-03-27.
  9. ^ "Nim Pascal Sources". Nim. GitHub. Retrieved 2013-04-05.
  10. ^ "Contributors". Nim. GitHub. Retrieved 2013-04-05.
  11. ^ Rumpf, Andreas (2014-01-15). Nimrod: A New Approach to Metaprogramming. InfoQ. Event occurs at 2:23. Retrieved 2014-07-20.
  12. ^ Hankiewicz, Grzegorz Adam (2014-03-10). "Nimrod for cross platform software". Rants from the Ballmer Peak. GitHub. Retrieved 2014-07-20.
  13. ^ "Nimrod-on-android failure". 2012-07-28. Retrieved 2015-02-28.
  14. ^ "Nim Syntax". akehrer. Retrieved 2015-03-27.
  15. ^ a b c "Nim Manual". Official website. Retrieved 2014-07-20.
  16. ^ "Strangeloop Nim presentation". Retrieved 2015-04-30.
  17. ^ "Nim's Garbage Collector". Nim documentation. Retrieved 2015-04-03.
  18. ^ "A Quick Comparison of Nim vs. Rust".
  19. ^ "What is special about Nim?". HookRace. 2015-01-01. Retrieved 2015-02-17.
  20. ^ "Nim Standard Library". Nim documentation. Retrieved 2015-04-04.
  21. ^ "Primary source code repository and bug tracker". GitHub. Retrieved 2015-05-04.
  22. ^ Nim "Forum". nim-lang.org. Retrieved 2015-05-04. {{cite web}}: Check |url= value (help)
  23. ^ "Nim at OSCON 2015". OSCON. 2015-07-20. Retrieved 2015-05-04.