Content deleted Content added
Fix links |
|||
(33 intermediate revisions by 16 users not shown) | |||
Line 1:
{{Short description|Programming language}}
{{use dmy dates|date=July 2022|cs1-dates=y}}
{{Infobox programming language
| name = Nim
| logo = Nim logo.svg
| logo caption = The Nim crown logo
| screenshot = <!-- Filename only -->
Line 30 ⟶ 28:
== Description ==
Nim is statically typed.<ref name="Nim Syntax">{{cite web |url=https://akehrer.github.io/posts/getting-started-with-nim |title=Nim Syntax |last=Kehrer |first=Aaron (akehrer) |website=GitHub |date=5 January 2015 |access-date=2015-01-05}}</ref> It supports compile-time [[metaprogramming]] features such as syntactic macros and [[Macro (computer science)#Syntactic macros|term rewriting macros]].<ref name="manual">{{cite web |url=https://nim-lang.org/docs/manual.html#term-rewriting-macros |title=Nim Manual |website=Nim-lang.org |access-date=2014-07-20}}</ref> Term rewriting macros enable [[Library (computing)|library]] implementations of common data structures, such as bignums and matrices, to be implemented efficiently and with syntactic integration, as if they were built-in language facilities.<ref name="strangeloop">{{cite web |url=https://thestrangeloop.com/sessions/nimrod-a-new-approach-to-meta-programming |archive-url=https://web.archive.org/web/20140713011227/https://thestrangeloop.com/sessions/nimrod-a-new-approach-to-meta-programming |url-status=dead |archive-date=2014-07-13 |title=Strangeloop Nim presentation |access-date=2015-04-30}}</ref> Iterators are supported and can be used as first class entities,<ref name="manual" /> as can functions, allowing for the use of [[functional programming]] methods. [[Object-oriented programming]] is supported by [[Inheritance (object-oriented programming)|inheritance]] and [[multiple dispatch]]. Functions can be generic and overloaded, and [[Generic programming|generics]] are further enhanced by Nim's support for [[
|author=Andrew Binstock, editor-in-chief of [[Dr. Dobb's Journal]], 2014
}}
Line 37 ⟶ 35:
== History ==
{| class="wikitable floatright" style="text-align:center; font-size: 85%;"
Line 104 ⟶ 101:
| 2021-10-19
|-
| {{Version |
| 2024-04-16
|-
Line 111 ⟶ 108:
| 2023-08-01
|-
| {{Version |
|
|-
| rowspan="2" | 2.2
| {{Version | o | 2.2.0}}
| 2024-10-02
|-
| {{Version | c | 2.2.4}}
| 2025-04-22
|-
| colspan="3" | {{Version | l | show=111100}}
Line 119 ⟶ 123:
|}
Andreas Rumpf is the designer and original implementer of Nim. He received a diploma in computer science from the [[University of Kaiserslautern-Landau]], [[Germany]]. His research interests include hard realtime systems, embedded systems, compiler construction and artificial intelligence.<ref>{{Cite book |last=Andreas Rumpf |title=Mastering Nim: A complete guide to the programming language}}</ref>
Nim's initial development was started in 2005 by Andreas Rumpf. It was originally named Nimrod when the project was made public in 2008.<ref name="NimAction">{{Cite book |last=Picheta |first=Dominik |year=2017 |title=Nim in Action |publisher=Manning Publications |isbn=978-1617293436}}</ref>{{rp|4–11}}<!-- Section 1.1: What is Nim? --> ▼
Nim's original website design by Dominik Picheta and Hugo Locurcio. Joseph Wecker created the Nim logo.
▲Nim's initial development was started in 2005 by Andreas Rumpf. It was originally named Nimrod when the project was made public in 2008.<ref name="NimAction">{{Cite book |last=Picheta |first=Dominik |year=2017 |title=Nim in Action |publisher=Manning Publications |isbn=978-1617293436}}</ref>{{rp|4–11}}<!-- Section 1.1: What is Nim? -->
The first version of the Nim [[compiler]] was written in [[Pascal (programming language)|Pascal]] using the [[Free Pascal]] compiler.<ref name="pas-sources">{{cite web |url=https://github.com/Araq/Nim/tree/ea1f1ec6d4d6c776eb0f81c2bebdd4cb4c817ebe/nim |title=Nim Pascal Sources |website=GitHub |access-date=2013-04-05}}</ref> In 2008, a version of the compiler written in Nim was released.<ref name="news">{{cite web |url=http://nim-lang.org:80/news.html |title=News |website=Nim-lang.org |access-date=2016-06-11 |url-status=live |archive-url=https://web.archive.org/web/20160626002904/http://nim-lang.org/news.html |archive-date=2016-06-26}}</ref> The compiler is [[free and open-source software]], and is being developed by a community of volunteers working with Andreas Rumpf.<ref name="contributors">{{cite web |url=https://github.com/Araq/Nim/contributors |title=Contributors |website=GitHub |access-date=2013-04-05}}</ref> The language was officially renamed from ''Nimrod'' to ''Nim'' with the release of version 0.10.2 in December 2014.<ref>{{cite web |url=https://nim-lang.org/blog/2014/12/29/version-0102-released.html |title=Version 0.10.2 released |date=2014-12-29 |first=Dominik |last=Picheta |website=Nim-lang.org |access-date=2018-10-17}}</ref> On September 23, 2019, version 1.0 of Nim was released, signifying the maturing of the language and its toolchain. On August
== Language design ==
=== Syntax ===
The syntax of Nim resembles that of [[Python (programming language)|Python]].<ref name="Yegulalp">{{cite web |url=https://www.infoworld.com/article/3157745/application-development/nim-language-draws-from-best-of-python-rust-go-and-lisp.html |title=Nim language draws from best of Python, Rust, Go, and Lisp |first=Serdar |last=Yegulalp |date=2017-01-16 |website=InfoWorld}}</ref> Code blocks and nesting statements are identified through use of [[Whitespace character|whitespace]], according to the [[Off-side rule|offside-rule]]. Many [[Reserved word|keywords]] are identical to their Python equivalents, which are mostly English keywords, whereas other programming languages usually use punctuation. With the goal of improving upon its influence languages, even though Nim supports [[indentation]]-based syntax like Python, it introduced additional flexibility. For example, a single [[
Unlike Python, Nim implements (native) static typing. Nim's type system allows for easy [[type conversion]], casting, and provides syntax for generic programming. Nim notably provides type classes which can stand in for multiple types, and provides several such type classes 'out of the box'. Type classes allow working with several types as if they were a single type. For example:
Line 136 ⟶ 143:
* <code>SomeOrdinal</code> {{En dash}} Represents all the basic countable and ordered types, except of non integer number
This code sample demonstrates the use of typeclasses in Nim
<syntaxhighlight lang="nim">
# Let's declare a function that takes any type of number and displays its double
Line 162 ⟶ 169:
=== Influence ===
According to the language creator, Nim was conceived to combine the best parts of Ada typing system, [[Python (programming language)|Python]] flexibility, and powerful [[Lisp (programming language)|Lisp]] macro system.<ref>{{Citation |title=Interview with Nim language creator Andreas Rumpf | date=9 March 2020 |url=https://www.youtube.com/watch?v=-9SGIB946lw |access-date=2023-10-15 |language=en}}</ref>
Nim was influenced by specific characteristics of existing languages, including the following:
Line 176 ⟶ 183:
*[[ParaSail (programming language)|ParaSail]]: pointer-free programming<ref name="parasail"/>
=== Uniform
Nim supports [[
For example, each of these lines print [["Hello, World!" program|"hello world"]], just with different syntax:
Line 198 ⟶ 205:
assert use_https == useHttps
</syntaxhighlight>
=== Stropping ===
The [[Stropping (syntax)|stropping]] feature allows the use of any name for variables or functions, even when the names are [[reserved word]]s for keywords. An example of stropping is the ability to define a variable named <code>if</code>, without clashing with the keyword <code>if</code>. Nim's implementation of this is achieved via backticks, allowing any reserved word to be used as an identifier.<ref>{{cite web |url=https://github.com/nim-lang/Nim/wiki/Tips-and-tricks |title=Tips and tricks |last1=Picheta |first1=Dominik (dom96) |last2=Wetherfordshire |first2=Billingsly (fowlmouth) |last3=Felsing |first3=Dennis (def-) |last4=Raaf |first4=Hans (oderwat) |last5=Dunn |first5=Christopher (cdunn2001) |author6=wizzardx |date=2017-10-25 |website=GitHub |access-date=2018-10-17}}</ref>
Line 230 ⟶ 238:
=== Memory management ===
Nim supports multiple memory management strategies, including the following:<ref>{{Cite web |title=Nim's Memory Management |url=https://nim-lang.org/docs/mm.html |access-date=
* <code>--
* <code>--gc:refc</code> – Standard deferred [[reference counting]] based [[Garbage collection (computer science)|garbage collector]] with a simple mark-and-sweep backup GC in order to collect cycles. Heaps are thread-local.▼
* <code>--
* <code>--
* <code>--
▲* <code>--
▲* <code>--gc:arc</code> – Automatic [[reference counting]] (ARC) with [[move semantics]] optimizations, offers a shared heap. It offers fully deterministic performance for hard realtime systems.<ref>{{Cite web |title=Introduction to ARC/ORC in Nim |url=https://nim-lang.org/blog/2020/10/15/introduction-to-arc-orc-in-nim.html |access-date=2023-08-17 |website=Nim Programming Language |language=en}}</ref> Reference cycles may cause memory leaks: these may be dealt with by manually annotating <code>{.acyclic.}</code> pragmas or by using <code>--gc:orc</code>.
* <code>--mm:go</code> – [[Go (programming language)|Go]]'s [[Garbage collection (computer science)|garbage collector]], useful for interoperability with [[Go (programming language)|Go]]. Offers a shared heap.
▲* <code>--gc:orc</code> – Same as <code>--gc:arc</code> but adds a cycle collector (the "O") based on "trial deletion".<ref>{{Cite web |title=ORC - Vorsprung durch Algorithmen |url=https://nim-lang.org/blog/2020/12/08/introducing-orc.html |access-date=2023-08-17 |website=Nim Programming Language |language=en}}</ref> The cycle collector only analyzes types if they are potentially cyclic.
* <code>--
As of Nim 2.0, ORC is the default GC.<ref
== Development tools ==
Line 322 ⟶ 330:
=== Use of other libraries ===
A Nim program can use any [[Library (computing)|library]] which can be used in a C, C++, or JavaScript program. [[Language binding]]s exist for many libraries, including [[GTK]],<ref>{{Citation|title=Installation|date=2021-09-25|url=https://github.com/nim-lang/gtk2|publisher=The Nim programming language|access-date=2021-11-16}}</ref><ref>{{Citation|last=StefanSalewski|title=High level GTK4 and GTK3 bindings for the Nim programming language|date=2021-11-15|url=https://github.com/StefanSalewski/gintro|access-date=2021-11-16}}</ref> [[Qt (software)|Qt]] QML,<ref>{{cite web |url=https://github.com/status-im/NimQml |title=NimQml |website=GitHub|date=10 November 2022 }}</ref> [[wxWidgets]],<ref>{{cite web |url=https://github.com/PMunch/wxnim |title=WxNim |website=GitHub|date=29 November 2022 }}</ref> [[Simple DirectMedia Layer|SDL 2]],<ref>{{Citation|title=SDL2 for Nim|date=2021-10-26|url=https://github.com/nim-lang/sdl2|publisher=The Nim programming language|access-date=2021-11-16}}</ref><ref>{{Citation|last=Arabadzhi|first=Vladimir|title=sdl2_nim 2.0.14.2|date=2021-11-15|url=https://github.com/Vladar4/sdl2_nim|access-date=2021-11-16}}</ref> [[Raylib]],<ref>{{cite web |url=https://github.com/planetis-m/naylib |title=naylib |website=GitHub|date=28 July 2024 }}</ref> [[
There are open source tools of various degree of support that can be used to interface Nim with [[Lua (programming language)|Lua]],<ref>{{cite web |url=https://github.com/jangko/nimLUA |title=nimLUA |last=Lim |first=Andri (jangko) |date=2018-10-17 |website=GitHub |access-date=2018-10-17}}</ref> [[Julia (programming language)|Julia]],<ref>{{cite web |date=24 August 2022 |title=NimJL |url=https://github.com/Clonkk/nimjl |website=GitHub}}</ref> [[Rust (programming language)|Rust]],<ref>{{cite web |url=https://github.com/arnetheduck/nbindgen |title=Nbindgen |website=GitHub|date=17 November 2022 }}</ref>
Line 444 ⟶ 452:
echo powersOfTwo.filter(greaterThan32)
</syntaxhighlight>
==== Side effects ====
Side effects of functions annotated with the <code>noSideEffect</code> pragma are checked, and the compiler will refuse to compile functions failing to meet those. Side effects in Nim include mutation, global state access or modification, asynchronous code, threaded code, and IO. Mutation of parameters may occur for functions taking parameters of <code>var</code> or <code>ref</code> type: this is expected to fail to compile with the currently-experimental <code>strictFuncs</code> in the future.<ref>{{Cite web |title=Nim Experimental Features: Strict Funcs |url=https://nim-lang.org/docs/manual_experimental.html#strict-funcs}}</ref> The <code>func</code> keyword introduces a shortcut for a <code>noSideEffect</code> pragma.<ref>{{Cite web |title=Nim Manual: Func |url=https://nim-lang.org/docs/manual.html#procedures-func}}</ref>
Line 473 ⟶ 482:
==== Function composition ====
[[
import std/[sequtils, sugar]
Line 482 ⟶ 491:
==== Algebraic data types and pattern matching ====
Nim has support for [[
import std/tables
Line 540 ⟶ 549:
==== Method calls and encapsulation ====
Nim's [[
<syntaxhighlight lang="nim"> type Socket* = ref object
host: int # private, lacks export marker
Line 603 ⟶ 613:
==== Generics ====
Nim supports both constrained and unconstrained generic programming.
Generics may be used in procedures, templates and macros. Unconstrained generic identifiers (<code>T</code> in this example) are defined after the routine's name in square brackets. Constrained generics can be placed on generic identifiers, or directly on parameters.
<syntaxhighlight lang="nim">
Line 738 ⟶ 748:
=== Conventions ===
The first Nim conference, NimConf, took place on June 20, 2020. It was held digitally due to [[COVID-19]], with an open call for contributor talks in the form of [[YouTube]] videos.<ref>{{cite web |url=https://nim-lang.org/blog/2020/05/14/nim-conference.html |title=Nim Online Conference 2020 |website=Nim |access-date=November 28, 2020}}</ref> The conference began with language overviews by Nim developers Andreas Rumpf and Dominik Picheta. Presentation topics included talks about web frameworks, [[mobile development]], [[Internet of things]] (IoT) devices, and [[game development]], including a talk about writing Nim for [[Game Boy Advance]].<ref>{{cite web |title=NimConf 2020 |url=https://conf.nim-lang.org/2020/ |access-date=August 17, 2023 |website=Nim}}</ref> NimConf 2020 is available as a YouTube playlist.<ref>{{cite web |url=https://www.youtube.com/playlist?list=PLxLdEZg8DRwTIEzUpfaIcBqhsj09mLWHx |title=NimConf 2020 Playlist |website=YouTube |access-date=November 28, 2020}}</ref> NimConf 2021 occurred the following year, was also held digitally, and included talks about [[Video game development|game development]], [[Read–eval–print loop|REPLs]], [[
In addition to official conferences, Nim has been featured at various other conventions. A presentation on Nim was given at the [[O'Reilly Open Source Convention]] (OSCON) in 2015.<ref>{{cite web |url=http://www.oscon.com/open-source-2015/public/schedule/detail/42497 |title=Nim at OSCON 2015 |date=2015-07-20 |website=[[O'Reilly Open Source Convention]] (OSCON) |publisher=O'Reilly Media |access-date=2018-10-17 |archive-date=2015-10-06 |archive-url=https://web.archive.org/web/20151006182449/http://www.oscon.com/open-source-2015/public/schedule/detail/42497 |url-status=dead }}</ref><ref>{{cite web |title=Essential Languages: Nim, Scala, Python |url=http://www.oreilly.com/pub/e/3420 |last1=Rumpf |first1=Andreas |last2=Swartz |first2=Jason |last3=Harrison |first3=Matt |website=O’Reilly |publisher=O'Reilly Media |access-date=2018-10-17}}</ref><ref>{{cite AV media |last=Rumpf |first=Andreas |date=2015-10-26 |title=OSCON 2015 – Nim: An Overview |medium=Video |language=en |url=https://www.youtube.com/watch?v=4rJEBs_Nnaw |access-date=2018-10-12 |website=YouTube}}</ref> Four speakers represented Nim at [[FOSDEM]] 2020, including the creator of the language, Andreas Rumpf.<ref>{{Cite web|url=https://fosdem.org/2020/schedule/events/|title=Events|website=fosdem.org|language=en|access-date=2020-02-17}}</ref> At FOSDEM 2022, Nim hosted their own developer room virtually due to the [[COVID-19 pandemic]].<ref>{{Cite web |title=Nim Devroom at FOSDEM 2022 - Call for Participation |url=https://nim-lang.org/blog/2021/12/02/fosdem-2022-cfp.html |access-date=2023-08-17 |website=Nim Programming Language |language=en}}</ref> Talks were held on [[Concurrency (computer science)|concurrency]], [[Embedded software|embedded programming]], programming for [[Graphics processing unit|GPUs]], [[Entity component system|entity-component systems]], [[Video game development|game development]], [[
== See also ==
{{Portal|Computer programming}}
* [[Crystal (programming language)]]
* [[D (programming language)]]
* [[Fat pointer]]
|