Nim (programming language): Difference between revisions

Content deleted Content added
Fix typo in spelling of Nim ("Nimr")
Dfelsing (talk | contribs)
Lots of cleanup, some minor additions
Line 11:
| latest_test_version = 0.10.2<ref name="news">{{cite web|url=http://nim-lang.org/news.html|title=News|work=Official website|accessdate=2015-01-02}}</ref>
| latest_test_date = 2014-12-29
| typing = [[static typing|static]]<ref name="nimbyex">{{cite web|url=http://nimrodnim-by-example.github.io/|title=NimrodNim by example|publisher=[[Github]]|accessdate=2014-07-20}}</ref>, [[strong typing|strong]]<ref name="nimrodbg">{{cite conference|url=http://ibob.github.io/slides/nimrodbg/#/|language=Russian|title=Метапрограмиране с Nimrod|last1=Караджов|first1=Захари|last2=Станимиров |first2=Борислав|year=2014|conference=VarnaConf|conferenceurl=http://varnaconf.com/|accessdate=2014-07-27}}</ref>, [[type inference|inferred]], [[structural type system|structural]]
| dialects =
| influenced_by = [[Ada (programming language)|Ada]], [[Modula 3]], [[Lisp (programming language)|Lisp]], [[C++ (programming language)|C++]], [[Object Pascal]], [[Python (programming language)|Python]]
| operating_system = [[Linux]], [[Mac OS X]], [[Microsoft Windows|Windows]], [[FreeBSD]], [[NetBSD]]
| license = [[MIT License]]<ref name='license'>{{cite web|url=http://www.nimrodnim-lang.org/question.html|title=FAQ|work=Official website|accessdate = 2014-07-20}}</ref><ref name='license1'>{{cite web|url=https://github.com/Araq/NimrodNim/blob/master/copying.txt|title=copying.txt|work=NimrodNim|publisher=[[Github]]|accessdate=2014-07-20}}</ref>
| website = {{URL|http://www.nim-lang.org/}}
}}
'''Nim''' (formerly known as Nimrod) is an [[imperative programming|imperative]], [[multi-paradigm programming language|multi-paradigm]], [[compiled programming language]]<ref name="drdobbs">{{cite web|url=http://www.drdobbs.com/open-source/nimrod-a-new-systems-programming-languag/240165321|title=Nimrod: A new systems programming language|first=Andreas|last=Rumpf|work=[[Dr. Dobb's Journal]]|date=2014-02-11|accessdate=2014-07-20}}</ref> designed and developed by Andreas Rumpf. It is designed to be an "efficient, expressive, and elegant" programming language,<ref name="nimrodnim-lang">{{cite web|url=http://nim-lang.org/|title=The NimrodNim Programming Language|work=Official website|accessdate=2014-07-20}}</ref> supporting [[metaprogramming]]<ref>{{cite web |url=http://developers.slashdot.org/story/15/02/15/0411217/nim-programming-language-gaining-traction?utm_source=slashdot&utm_medium=twitter |title=Nim Programming Language Gaining Traction |publisher=Slashdot anonymous poster|accessdate=15 Feb 2014}}</ref>, [[functional programming|functional]], [[message passing]],<ref name="concurrency">{{cite web|url=http://www.nimrodnim-lang.org/question.html|title=FAQ|work=Official website|accessdate=2013-04-05}}</ref> [[Procedural programming|procedural]], and [[Object-oriented programming|object-oriented]] programming styles.
 
The Nim compiler was initially written in Pascal,<ref name="pas-sources">{{cite web|url=https://github.com/Araq/NimrodNim/tree/ea1f1ec6d4d6c776eb0f81c2bebdd4cb4c817ebe/nim|title=NimrodNim pascalPascal sourcesSources|work=NimrodNim|publisher=[[Github]]|accessdate=2013-04-05}}</ref> in 2008<ref name="news"/> 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.<ref name="contributors">{{cite web|url=https://github.com/Araq/NimrodNim/contributors|title=Contributors|work=NimrodNim|publisher=[[Github]]|accessdate=2013-04-05}}</ref> The compiler generates optimized C code and defers compilation to an external compiler<ref name="infoq">{{cite video|url=http://www.infoq.com/presentations/nimrod|title=Nimrod: A New Approach to Metaprogramming|first=Andreas|last=Rumpf|work=[[InfoQ]]|time=2:23|date=2014-01-15|accessdate=2014-07-20}}</ref> (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<ref name="drdobbs"/>, this in turn allows Nim to be used to write iOS as well as Android applications<ref name="gradhacrossplatform">{{cite web|url=http://gradha.github.io/articles/2014/03/nimrod-for-cross-platform-software.html|title=Nimrod for cross platform software|first=Grzegorz Adam|last=Hankiewicz|work=Rants from the Ballmer Peak|publisher=[[Github]]|date=2014-03-10|accessdate=2014-07-20}}</ref>
 
== Description ==
Line 26:
Nim's syntax is an unusual blend between Python and Pascal<ref>{{Cite web|url = http://www.drdobbs.com/jvm/the-rise-and-fall-of-languages-in-2013/240165192|title = The Rise And Fall of Languages in 2013|date = 2014-01-07|accessdate = |website = |publisher = Dr. Dobb's|last = Binstock|first = Andrew}}</ref>. The language shares many syntactical similarities to Python<ref name="pichetablog">{{cite web|url=http://picheta.me/articles/2013/10/about-nimrods-features.html|title=About Nimrod's features|first=Dominik|last=Picheta|work=Blog|date=2013-10-27|accessdate=2014-07-20}}</ref> (for example the <code>if</code> and <code>for</code> 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,<ref name="manual">{{cite web|url=http://nim-lang.org/manual.html|title=NimrodNim Manual|work=Official website|accessdate=2014-07-20}}</ref> the combination of these features is largely unique to NimrodNim.<ref name="trforum">{{cite web|url=http://forum.nimrodnim-lang.org/t/70|title=Term rewriting macros|last=Araq|work=NimrodNim Forum|date=2012-09-10|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. 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"/>
 
== Examples ==
 
The following code examples are valid as of Nim 0.910.42. Syntax and semantics may change in subsequent versions.
 
=== [[Hello world]]<ref name="drdobbs" /> ===
Line 42 ⟶ 41:
<syntaxhighlight lang="python">
proc reverse(s: string): string =
result = "" # implicit result variable
for i in countdown(high(s), 0):
result.add s[i]
Line 54 ⟶ 53:
 
<syntaxhighlight lang="python">
template GenTypegenType(name, fieldname: expr, fieldtype: typedesc) =
type
name = object
fieldname: fieldtype
 
GenTypegenType(TTestTest, foo, int)
 
var x = TTestTest(foo: 4566)
echo(x.foo) # 4566
</syntaxhighlight>
This is an example of metaprogramming in Nim using its template facilities. The <code>GenTypegenType</code> is invoked at compile-time and a <code>TTestTest</code> type is created.
 
=== Wrapping C functions ===
 
<syntaxhighlight lang="python">
proc printf(formatstr: cstring)
{.header: "<stdio.h>", varargs.}
 
printf("%s %d\n", "foo", 5)
</syntaxhighlight>
Existing C code can directly be used in Nim. In this code the well known <code>printf</code> function is imported into Nim and subsequently used.<ref name="special">{{cite web|url=http://hookrace.net/blog/what-is-special-about-nim/|title=What is special about Nim?|work=HookRace|accessdate=2015-02-17}}</ref>
 
== References ==