rst_examples.html 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. <?xml version="1.0" encoding="utf-8" ?>
  2. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "https://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  3. <!-- This file is generated by Nim. -->
  4. <html xmlns="https://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
  5. <head>
  6. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  7. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  8. <title>Not a Nim Manual</title>
  9. <!-- Favicon -->
  10. <link rel="shortcut icon" href="data:image/x-icon;base64,AAABAAEAEBAAAAEAIABoBAAAFgAAACgAAAAQAAAAIAAAAAEAIAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAD///8A////AP///wD///8A////AP///wD///8A////AP///wD///8A////AP///wD///8A////AP///wD///8A////AP///wD///8A////AP///wD///8A////AAAAAAUAAAAF////AP///wD///8A////AP///wD///8A////AP///wD///8A////AAAAAAIAAABbAAAAlQAAAKIAAACbAAAAmwAAAKIAAACVAAAAWwAAAAL///8A////AP///wD///8A////AAAAABQAAADAAAAAYwAAAA3///8A////AP///wD///8AAAAADQAAAGMAAADAAAAAFP///wD///8A////AP///wAAAACdAAAAOv///wD///8A////AP///wD///8A////AP///wD///8AAAAAOgAAAJ3///8A////AP///wAAAAAnAAAAcP///wAAAAAoAAAASv///wD///8A////AP///wAAAABKAAAAKP///wAAAABwAAAAJ////wD///8AAAAAgQAAABwAAACIAAAAkAAAAJMAAACtAAAAFQAAABUAAACtAAAAkwAAAJAAAACIAAAAHAAAAIH///8A////AAAAAKQAAACrAAAAaP///wD///8AAAAARQAAANIAAADSAAAARf///wD///8AAAAAaAAAAKsAAACk////AAAAADMAAACcAAAAnQAAABj///8A////AP///wAAAAAYAAAAGP///wD///8A////AAAAABgAAACdAAAAnAAAADMAAAB1AAAAwwAAAP8AAADpAAAAsQAAAE4AAAAb////AP///wAAAAAbAAAATgAAALEAAADpAAAA/wAAAMMAAAB1AAAAtwAAAOkAAAD/AAAA/wAAAP8AAADvAAAA3gAAAN4AAADeAAAA3gAAAO8AAAD/AAAA/wAAAP8AAADpAAAAtwAAAGUAAAA/AAAA3wAAAP8AAAD/AAAA/wAAAP8AAAD/AAAA/wAAAP8AAAD/AAAA/wAAAP8AAADfAAAAPwAAAGX///8A////AAAAAEgAAADtAAAAvwAAAL0AAADGAAAA7wAAAO8AAADGAAAAvQAAAL8AAADtAAAASP///wD///8A////AP///wD///8AAAAAO////wD///8A////AAAAAIcAAACH////AP///wD///8AAAAAO////wD///8A////AP///wD///8A////AP///wD///8A////AP///wD///8A////AP///wD///8A////AP///wD///8A////AP///wD///8A////AP///wD///8A////AP///wD///8A////AP///wD///8A////AP///wD///8A////AP///wD///8A//8AAP//AAD4HwAA7/cAAN/7AAD//wAAoYUAAJ55AACf+QAAh+EAAAAAAADAAwAA4AcAAP5/AAD//wAA//8AAA=="/>
  11. <link rel="icon" type="image/png" sizes="32x32" href="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAACAAAAAgCAYAAABzenr0AAAABmJLR0QA/wD/AP+gvaeTAAAACXBIWXMAAA3XAAAN1wFCKJt4AAAAB3RJTUUH4QQQEwksSS9ZWwAAAk1JREFUWMPtll2ITVEUx39nn/O7Y5qR8f05wtCUUr6ZIS++8pEnkZInPImneaCQ5METNdOkeFBKUhMPRIkHKfEuUZSUlGlKPN2TrgfncpvmnntnmlEyq1Z7t89/rf9a6+y99oZxGZf/XeIq61EdtgKXgdXA0xrYAvBjOIF1AI9zvjcC74BSpndrJPkBWDScTF8Aa4E3wDlgHbASaANmVqlcCnwHvgDvgVfAJ+AikAAvgfVZwLnSVZHZaOuKoQi3ZOMi4NkYkpe1p4J7A8BpYAD49hfIy/oqG0+hLomiKP2L5L+1ubn5115S+3OAn4EnwBlgMzCjyt6ZAnQCJ4A7wOs88iRJHvw50HoujuPBoCKwHWiosy8MdfZnAdcHk8dxXFJ3VQbQlCTJvRBCGdRbD4M6uc5glpY3eAihpN5S5w12diSEcCCEcKUO4ljdr15T76ur1FDDLIQQ3qv71EdDOe3Kxj3leRXyk+pxdWnFWod6Wt2bY3de3aSuUHcPBVimHs7mK9WrmeOF6lR1o9qnzskh2ar2qm1qizpfXaPeVGdlmGN5pb09qMxz1Xb1kLqgzn1RyH7JUXW52lr5e/Kqi9qpto7V1atuUzfnARrV7jEib1T76gG2qxdGmXyiekkt1GswPTtek0aBfJp6YySGBfWg2tPQ0FAYgf1stUfdmdcjarbYJEniKIq6gY/Aw+zWHAC+p2labGpqiorFYgGYCEzN7oQdQClN07O1/EfDyGgC0ALMBdYAi4FyK+4H3gLPsxfR1zRNi+NP7nH5J+QntnXe5B5mpfQAAAAASUVORK5CYII=">
  12. <!-- CSS -->
  13. <link rel="stylesheet" type="text/css" href="nimdoc.out.css">
  14. <!-- JS -->
  15. <script type="text/javascript" src="dochack.js"></script>
  16. </head>
  17. <body>
  18. <div class="document" id="documentId">
  19. <div class="container">
  20. <h1 class="title">Not a Nim Manual</h1>
  21. <div class="row">
  22. <div class="three columns">
  23. <div class="theme-select-wrapper">
  24. <label for="theme-select">Theme:&nbsp;</label>
  25. <select id="theme-select" onchange="setTheme(this.value)">
  26. <option value="auto">🌗 Match OS</option>
  27. <option value="dark">🌑 Dark</option>
  28. <option value="light">🌕 Light</option>
  29. </select>
  30. </div>
  31. <div id="global-links">
  32. <ul class="simple">
  33. <li><a id="indexLink" href="theindex.html">Index</a></li>
  34. </ul>
  35. </div>
  36. <div id="searchInputDiv">
  37. Search: <input type="search" id="searchInput" oninput="search()"/>
  38. </div>
  39. <div>
  40. Group by:
  41. <select onchange="groupBy(this.value)">
  42. <option value="section">Section</option>
  43. <option value="type">Type</option>
  44. </select>
  45. </div>
  46. <ul class="simple simple-toc" id="toc-list">
  47. <li><a class="reference" id="about-this-document_toc" href="#about-this-document">About this document</a></li>
  48. <ul class="simple"><li><a class="reference" id="about-this-document-encoding_toc" href="#about-this-document-encoding">Encoding</a></li>
  49. <li><a class="reference" id="about-this-document-indentation_toc" href="#about-this-document-indentation">Indentation</a></li>
  50. <li><a class="reference" id="about-this-document-operators_toc" href="#about-this-document-operators">Operators</a></li>
  51. </ul><li><a class="reference" id="constants-and-constant-expressions_toc" href="#constants-and-constant-expressions">Constants and Constant Expressions</a></li>
  52. <ul class="simple"><li><a class="reference" id="constants-and-constant-expressions-symbol-lookup-in-generics_toc" href="#constants-and-constant-expressions-symbol-lookup-in-generics">Symbol lookup in generics</a></li>
  53. <ul class="simple"><li><a class="reference" id="symbol-lookup-in-generics-open-and-closed-symbols_toc" href="#symbol-lookup-in-generics-open-and-closed-symbols">Open and Closed symbols</a></li>
  54. <li><a class="reference" id="symbol-lookup-in-generics-collective-imports-from-a-directory_toc" href="#symbol-lookup-in-generics-collective-imports-from-a-directory">Collective imports from a directory</a></li>
  55. </ul></ul><li><a class="reference" id="pragmas_toc" href="#pragmas">Pragmas</a></li>
  56. <ul class="simple"><li><a class="reference" id="pragmas-deprecated-pragma_toc" href="#pragmas-deprecated-pragma">deprecated pragma</a></li>
  57. </ul><li><a class="reference" id="pure-libraries_toc" href="#pure-libraries">Pure libraries</a></li>
  58. <ul class="simple"><li><a class="reference" id="pure-libraries-automatic-imports_toc" href="#pure-libraries-automatic-imports">Automatic imports</a></li>
  59. </ul><li><a class="reference" id="code-reordering_toc" href="#code-reordering">Code reordering</a></li>
  60. <ul class="simple"><li><a class="reference" id="code-reordering-parameter-constraints_toc" href="#code-reordering-parameter-constraints">Parameter constraints</a></li>
  61. <ul class="simple"><li><a class="reference" id="parameter-constraints-the-tilde-operator_toc" href="#parameter-constraints-the-tilde-operator">The <tt class="docutils literal"><span class="pre">~</span></tt> operator</a></li>
  62. <li><a class="reference" id="parameter-constraints-the-starstar-operator_toc" href="#parameter-constraints-the-starstar-operator">The <tt class="docutils literal"><span class="pre">**</span></tt> operator</a></li>
  63. </ul><li><a class="reference" id="code-reordering-time-measurement-with-garbage-collectors_toc" href="#code-reordering-time-measurement-with-garbage-collectors">Time measurement with garbage collectors</a></li>
  64. </ul><li><a class="reference" id="introduction_toc" href="#introduction">Introduction</a></li>
  65. </ul>
  66. </div>
  67. <div class="nine columns" id="content">
  68. <div id="tocRoot"></div>
  69. <p class="module-desc"><table class="docinfo" frame="void" rules="none"><col class="docinfo-name" /><col class="docinfo-content" /><tbody valign="top"><tr><th class="docinfo-name">Authors:</th><td>Andreas Rumpf, Zahary Karadjov</td></tr>
  70. <tr><th class="docinfo-name">Version:</th><td>|nimversion|</td></tr>
  71. </tbody></table><blockquote><p>&quot;Complexity&quot; seems to be a lot like &quot;energy&quot;: you can transfer it from the end-user to one/some of the other players, but the total amount seems to remain pretty much constant for a given task. -- Ran</p></blockquote>
  72. <h1><a class="toc-backref" id="about-this-document" href="#about-this-document">About this document</a></h1><p><strong>Note</strong>: This document is a draft! Several of Nim's features may need more precise wording. This manual is constantly evolving into a proper specification.</p>
  73. <p><strong>Note</strong>: The experimental features of Nim are covered <a class="reference external" href="manual_experimental.html">here</a>.</p>
  74. <p><strong>Note</strong>: Assignments, moves, and destruction are specified in the <a class="reference external" href="destructors.html">destructors</a> document.</p>
  75. <p>The language constructs are explained using an extended BNF, in which <tt class="docutils literal"><span class="pre">(a)*</span></tt> means 0 or more <tt class="docutils literal"><span class="pre">a</span></tt>'s, <tt class="docutils literal"><span class="pre">a+</span></tt> means 1 or more <tt class="docutils literal"><span class="pre">a</span></tt>'s, and <tt class="docutils literal"><span class="pre">(a)?</span></tt> means an optional <em>a</em>. Parentheses may be used to group elements.</p>
  76. <p><tt class="docutils literal"><span class="pre">&amp;</span></tt> is the lookahead operator; <tt class="docutils literal"><span class="pre">&amp;a</span></tt> means that an <tt class="docutils literal"><span class="pre">a</span></tt> is expected but not consumed. It will be consumed in the following rule.</p>
  77. <p>Non-terminals start with a lowercase letter, abstract terminal symbols are in UPPERCASE. Verbatim terminal symbols (including keywords) are quoted with <tt class="docutils literal"><span class="pre">'</span></tt>. An example:<pre>ifStmt = 'if' expr ':' stmts ('elif' expr ':' stmts)* ('else' stmts)?</pre>
  78. </p>
  79. <p>In a typical Nim program, most of the code is compiled into the executable. However, some of the code may be executed at <span id="compileminustime_1">compile-time</span>. This can include constant expressions, macro definitions, and Nim procedures used by macro definitions. Most of the Nim language is supported at compile-time, but there are some restrictions -- see <a class="reference external" href="#restrictions-on-compileminustime-execution">Restrictions on Compile-Time Execution</a> for details. We use the term <span id="runtime_1">runtime</span> to cover both compile-time execution and code execution in the executable.</p>
  80. <pre class="listing"><span class="Keyword">var</span> <span class="Identifier">a</span><span class="Punctuation">:</span> <span class="Identifier">array</span><span class="Punctuation">[</span><span class="FloatNumber">0.</span><span class="Operator">.</span><span class="DecNumber">1</span><span class="Punctuation">,</span> <span class="Identifier">char</span><span class="Punctuation">]</span>
  81. <span class="Keyword">let</span> <span class="Identifier">i</span> <span class="Operator">=</span> <span class="DecNumber">5</span>
  82. <span class="Keyword">try</span><span class="Punctuation">:</span>
  83. <span class="Identifier">a</span><span class="Punctuation">[</span><span class="Identifier">i</span><span class="Punctuation">]</span> <span class="Operator">=</span> <span class="CharLit">'N'</span>
  84. <span class="Keyword">except</span> <span class="Identifier">IndexDefect</span><span class="Punctuation">:</span>
  85. <span class="Identifier">echo</span> <span class="StringLit">&quot;invalid index&quot;</span></pre>
  86. <h2><a class="toc-backref" id="about-this-document-encoding" href="#about-this-document-encoding">Encoding</a></h2><p>All Nim source files are in the UTF-8 encoding (or its ASCII subset). Other encodings are not supported. Any of the standard platform line termination sequences can be used - the Unix form using ASCII LF (linefeed), the Windows form using the ASCII sequence CR LF (return followed by linefeed), or the old Macintosh form using the ASCII CR (return) character. All of these forms can be used equally, regardless of the platform.</p>
  87. <h2><a class="toc-backref" id="about-this-document-indentation" href="#about-this-document-indentation">Indentation</a></h2><p>Nim's standard grammar describes an <span id="indentation-sensitive_1">indentation sensitive</span> language. This means that all the control structures are recognized by indentation. Indentation consists only of spaces; tabulators are not allowed.</p>
  88. <p>With this notation we can now easily define the core of the grammar: A block of statements (simplified example):<pre>ifStmt = 'if' expr ':' stmt
  89. (IND{=} 'elif' expr ':' stmt)*
  90. (IND{=} 'else' ':' stmt)?
  91. simpleStmt = ifStmt / ...
  92. stmt = IND{&gt;} stmt ^+ IND{=} DED # list of statements
  93. / simpleStmt # or a simple statement</pre>
  94. </p>
  95. <p>String literals can be delimited by matching double quotes, and can contain the following <span id="escape-sequences_1">escape sequences</span>:</p>
  96. <table border="1" class="docutils"><tr><th>Escape sequence</th><th>Meaning</th></tr>
  97. <tr><td><tt class="docutils literal"><span class="pre">\p</span></tt></td><td>platform specific newline: CRLF on Windows, LF on Unix</td></tr>
  98. <tr><td><tt class="docutils literal"><span class="pre">\r</span></tt>, <tt class="docutils literal"><span class="pre">\c</span></tt></td><td><span id="carriage-return_1">carriage return</span></td></tr>
  99. <tr><td><tt class="docutils literal"><span class="pre">\n</span></tt>, <tt class="docutils literal"><span class="pre">\l</span></tt></td><td><span id="line-feed_1">line feed</span> (often called <span id="newline_1">newline</span>)</td></tr>
  100. <tr><td><tt class="docutils literal"><span class="pre">\f</span></tt></td><td><span id="form-feed_1">form feed</span></td></tr>
  101. <tr><td><tt class="docutils literal"><span class="pre">\t</span></tt></td><td><span id="tabulator_1">tabulator</span></td></tr>
  102. <tr><td><tt class="docutils literal"><span class="pre">\v</span></tt></td><td><span id="vertical-tabulator_1">vertical tabulator</span></td></tr>
  103. <tr><td><tt class="docutils literal"><span class="pre">\\</span></tt></td><td><span id="backslash_1">backslash</span></td></tr>
  104. <tr><td><tt class="docutils literal"><span class="pre">\&quot;</span></tt></td><td><span id="quotation-mark_1">quotation mark</span></td></tr>
  105. <tr><td><tt class="docutils literal"><span class="pre">\'</span></tt></td><td><span id="apostrophe_1">apostrophe</span></td></tr>
  106. <tr><td><tt class="docutils literal"><span class="pre">\</span></tt> '0'..'9'+</td><td><span id="character-with-decimal-value-d_1">character with decimal value d</span>; all decimal digits directly following are used for the character</td></tr>
  107. <tr><td><tt class="docutils literal"><span class="pre">\a</span></tt></td><td><span id="alert_1">alert</span></td></tr>
  108. <tr><td><tt class="docutils literal"><span class="pre">\b</span></tt></td><td><span id="backspace_1">backspace</span></td></tr>
  109. <tr><td><tt class="docutils literal"><span class="pre">\e</span></tt></td><td><span id="escape_1">escape</span> <span id="esc_1">[ESC]</span></td></tr>
  110. <tr><td><tt class="docutils literal"><span class="pre">\x</span></tt> HH</td><td><span id="character-with-hex-value-hh_1">character with hex value HH</span>; exactly two hex digits are allowed</td></tr>
  111. <tr><td><tt class="docutils literal"><span class="pre">\u</span></tt> HHHH</td><td><span id="unicode-codepoint-with-hex-value-hhhh_1">unicode codepoint with hex value HHHH</span>; exactly four hex digits are allowed</td></tr>
  112. <tr><td><tt class="docutils literal"><span class="pre">\u</span></tt> {H+}</td><td><span id="unicode-codepoint_1">unicode codepoint</span>; all hex digits enclosed in <tt class="docutils literal"><span class="pre">{}</span></tt> are used for the codepoint</td></tr>
  113. </table><pre class="listing"><span class="LongStringLit">&quot;&quot;&quot;&quot;long string within quotes&quot;&quot;&quot;&quot;</span></pre><p>Produces:<pre>&quot;long string within quotes&quot;</pre>
  114. </p>
  115. <h2><a class="toc-backref" id="about-this-document-operators" href="#about-this-document-operators">Operators</a></h2><p>Nim allows user defined operators. An operator is any combination of the following characters:<pre>= + - * / &lt; &gt;
  116. @ $ ~ &amp; % |
  117. ! ? ^ . : \</pre>
  118. </p>
  119. <p>(The grammar uses the terminal OPR to refer to operator symbols as defined here.)</p>
  120. <p>The following strings denote other tokens:<pre>` ( ) { } [ ] , ; [. .] {. .} (. .) [:</pre>
  121. </p>
  122. <p>Otherwise, precedence is determined by the first character.</p>
  123. <table border="1" class="docutils"><tr><th>Precedence level</th><th>Operators</th><th>First character</th><th>Terminal symbol</th></tr>
  124. <tr><td>10 (highest)</td><td></td><td><tt class="docutils literal"><span class="pre">$ ^</span></tt></td><td>OP10</td></tr>
  125. <tr><td>9</td><td><tt class="docutils literal"><span class="pre">* / div mod shl shr %</span></tt></td><td><tt class="docutils literal"><span class="pre">* % \ /</span></tt></td><td>OP9</td></tr>
  126. <tr><td>8</td><td><tt class="docutils literal"><span class="pre">+ -</span></tt></td><td><tt class="docutils literal"><span class="pre">+ - ~ |</span></tt></td><td>OP8</td></tr>
  127. <tr><td>7</td><td><tt class="docutils literal"><span class="pre">&amp;</span></tt></td><td><tt class="docutils literal"><span class="pre">&amp;</span></tt></td><td>OP7</td></tr>
  128. <tr><td>6</td><td><tt class="docutils literal"><span class="pre">..</span></tt></td><td><tt class="docutils literal"><span class="pre">.</span></tt></td><td>OP6</td></tr>
  129. <tr><td>5</td><td><tt class="docutils literal"><span class="pre">== &lt;= &lt; &gt;= &gt; != in notin is isnot not of as from</span></tt></td><td><tt class="docutils literal"><span class="pre">= &lt; &gt; !</span></tt></td><td>OP5</td></tr>
  130. <tr><td>4</td><td><tt class="docutils literal"><span class="pre">and</span></tt></td><td></td><td>OP4</td></tr>
  131. <tr><td>3</td><td><tt class="docutils literal"><span class="pre">or xor</span></tt></td><td></td><td>OP3</td></tr>
  132. <tr><td>2</td><td></td><td><tt class="docutils literal"><span class="pre">@ : ?</span></tt></td><td>OP2</td></tr>
  133. <tr><td>1</td><td><em>assignment operator</em> (like <tt class="docutils literal"><span class="pre">+=</span></tt>, <tt class="docutils literal"><span class="pre">*=</span></tt>)</td><td></td><td>OP1</td></tr>
  134. <tr><td>0 (lowest)</td><td><em>arrow like operator</em> (like <tt class="docutils literal"><span class="pre">-&gt;</span></tt>, <tt class="docutils literal"><span class="pre">=&gt;</span></tt>)</td><td></td><td>OP0</td></tr>
  135. </table>
  136. <h1><a class="toc-backref" id="constants-and-constant-expressions" href="#constants-and-constant-expressions">Constants and Constant Expressions</a></h1><p>A <span id="constant_1">constant</span> is a symbol that is bound to the value of a constant expression. Constant expressions are restricted to depend only on the following categories of values and operations, because these are either built into the language or declared and evaluated before semantic analysis of the constant expression:</p>
  137. <ul class="simple"><li>literals</li>
  138. <li>built-in operators</li>
  139. <li>previously declared constants and compile-time variables</li>
  140. <li>previously declared macros and templates</li>
  141. <li>previously declared procedures that have no side effects beyond possibly modifying compile-time variables</li>
  142. </ul>
  143. <p>These integer types are pre-defined:</p>
  144. <dl class="docutils"><dt><tt class="docutils literal"><span class="pre">int</span></tt></dt>
  145. <dd>the generic signed integer type; its size is platform-dependent and has the same size as a pointer. This type should be used in general. An integer literal that has no type suffix is of this type if it is in the range <tt class="docutils literal"><span class="pre">low(int32)..high(int32)</span></tt> otherwise the literal's type is <tt class="docutils literal"><span class="pre">int64</span></tt>.</dd>
  146. <dt>intXX</dt>
  147. <dd>additional signed integer types of XX bits use this naming scheme (example: int16 is a 16-bit wide integer). The current implementation supports <tt class="docutils literal"><span class="pre">int8</span></tt>, <tt class="docutils literal"><span class="pre">int16</span></tt>, <tt class="docutils literal"><span class="pre">int32</span></tt>, <tt class="docutils literal"><span class="pre">int64</span></tt>. Literals of these types have the suffix 'iXX.</dd>
  148. <dt><tt class="docutils literal"><span class="pre">uint</span></tt></dt>
  149. <dd>the generic <span id="unsigned-integer_1">unsigned integer</span> type; its size is platform-dependent and has the same size as a pointer. An integer literal with the type suffix <tt class="docutils literal"><span class="pre">'u</span></tt> is of this type.</dd>
  150. </dl>
  151. <p>Let <tt class="docutils literal"><span class="pre">T</span></tt>'s be <tt class="docutils literal"><span class="pre">p</span></tt>'s return type. NRVO applies for <tt class="docutils literal"><span class="pre">T</span></tt> if <tt class="docutils literal"><span class="pre">sizeof(T) &gt;= N</span></tt> (where <tt class="docutils literal"><span class="pre">N</span></tt> is implementation dependent), in other words, it applies for &quot;big&quot; structures.</p>
  152. <p>Apart from built-in operations like array indexing, memory allocation, etc. the <tt class="docutils literal"><span class="pre">raise</span></tt> statement is the only way to raise an exception.</p>
  153. <p><tt class="docutils literal"><span class="pre"><span class="Identifier">typedesc</span></span></tt> used as a parameter type also introduces an implicit generic. <tt class="docutils literal"><span class="pre"><span class="Identifier">typedesc</span></span></tt> has its own set of rules:</p>
  154. <p>The <tt class="docutils literal"><span class="pre">!=</span></tt>, <tt class="docutils literal"><span class="pre">&gt;</span></tt>, <tt class="docutils literal"><span class="pre">&gt;=</span></tt>, <tt class="docutils literal"><span class="pre">in</span></tt>, <tt class="docutils literal"><span class="pre">notin</span></tt>, <tt class="docutils literal"><span class="pre">isnot</span></tt> operators are in fact templates:</p>
  155. <p><tt class="docutils literal"><span class="pre">a &gt; b</span></tt> is transformed into <tt class="docutils literal"><span class="pre">b &lt; a</span></tt>.<br/><tt class="docutils literal"><span class="pre">a in b</span></tt> is transformed into <tt class="docutils literal"><span class="pre">contains(b, a)</span></tt>.<br/><tt class="docutils literal"><span class="pre">notin</span></tt> and <tt class="docutils literal"><span class="pre">isnot</span></tt> have the obvious meanings.<br/></p><p>A template where every parameter is <tt class="docutils literal"><span class="pre">untyped</span></tt> is called an <span id="immediate_1">immediate</span> template. For historical reasons templates can be explicitly annotated with an <tt class="docutils literal"><span class="pre">immediate</span></tt> pragma and then these templates do not take part in overloading resolution and the parameters' types are <em>ignored</em> by the compiler. Explicit immediate templates are now deprecated.</p>
  156. <h2><a class="toc-backref" id="constants-and-constant-expressions-symbol-lookup-in-generics" href="#constants-and-constant-expressions-symbol-lookup-in-generics">Symbol lookup in generics</a></h2>
  157. <h3><a class="toc-backref" id="symbol-lookup-in-generics-open-and-closed-symbols" href="#symbol-lookup-in-generics-open-and-closed-symbols">Open and Closed symbols</a></h3><p>The symbol binding rules in generics are slightly subtle: There are &quot;open&quot; and &quot;closed&quot; symbols. A &quot;closed&quot; symbol cannot be re-bound in the instantiation context, an &quot;open&quot; symbol can. Per default overloaded symbols are open and every other symbol is closed.</p>
  158. <p>In templates identifiers can be constructed with the backticks notation:</p>
  159. <pre class="listing"><span class="Keyword">template</span> <span class="Identifier">typedef</span><span class="Punctuation">(</span><span class="Identifier">name</span><span class="Punctuation">:</span> <span class="Identifier">untyped</span><span class="Punctuation">,</span> <span class="Identifier">typ</span><span class="Punctuation">:</span> <span class="Identifier">typedesc</span><span class="Punctuation">)</span> <span class="Operator">=</span>
  160. <span class="Keyword">type</span>
  161. <span class="Punctuation">`</span><span class="Identifier">T</span> <span class="Identifier">name</span><span class="Punctuation">`</span><span class="Operator">*</span> <span class="Punctuation">{</span><span class="Operator">.</span><span class="Identifier">inject</span><span class="Operator">.</span><span class="Punctuation">}</span> <span class="Operator">=</span> <span class="Identifier">typ</span>
  162. <span class="Punctuation">`</span><span class="Identifier">P</span> <span class="Identifier">name</span><span class="Punctuation">`</span><span class="Operator">*</span> <span class="Punctuation">{</span><span class="Operator">.</span><span class="Identifier">inject</span><span class="Operator">.</span><span class="Punctuation">}</span> <span class="Operator">=</span> <span class="Keyword">ref</span> <span class="Punctuation">`</span><span class="Identifier">T</span> <span class="Identifier">name</span><span class="Punctuation">`</span>
  163. <span class="Identifier">typedef</span><span class="Punctuation">(</span><span class="Identifier">myint</span><span class="Punctuation">,</span> <span class="Identifier">int</span><span class="Punctuation">)</span>
  164. <span class="Keyword">var</span> <span class="Identifier">x</span><span class="Punctuation">:</span> <span class="Identifier">PMyInt</span></pre><p>In the example <tt class="docutils literal"><span class="pre">name</span></tt> is instantiated with <tt class="docutils literal"><span class="pre">myint</span></tt>, so `T name` becomes <tt class="docutils literal"><span class="pre">Tmyint</span></tt>.</p>
  165. <p>Only top-level symbols that are marked with an asterisk (<tt class="docutils literal"><span class="pre">*</span></tt>) are exported.</p>
  166. <p>The algorithm for compiling modules is:</p>
  167. <ul class="simple"><li>compile the whole module as usual, following import statements recursively</li>
  168. <li>if there is a cycle only import the already parsed symbols (that are exported); if an unknown identifier occurs then abort</li>
  169. </ul>
  170. <h3><a class="toc-backref" id="symbol-lookup-in-generics-collective-imports-from-a-directory" href="#symbol-lookup-in-generics-collective-imports-from-a-directory">Collective imports from a directory</a></h3><p>The syntax <tt class="docutils literal"><span class="pre">import dir / [moduleA, moduleB]</span></tt> can be used to import multiple modules from the same directory.</p>
  171. <h1><a class="toc-backref" id="pragmas" href="#pragmas">Pragmas</a></h1><p>Pragmas are Nim's method to give the compiler additional information / commands without introducing a massive number of new keywords. Pragmas are processed on the fly during semantic checking. Pragmas are enclosed in the special <tt class="docutils literal"><span class="pre">{.</span></tt> and <tt class="docutils literal"><span class="pre">.}</span></tt> curly brackets. Pragmas are also often used as a first implementation to play with a language feature before a nicer syntax to access the feature becomes available.</p>
  172. <h2><a class="toc-backref" id="pragmas-deprecated-pragma" href="#pragmas-deprecated-pragma">deprecated pragma</a></h2><p>The deprecated pragma is used to mark a symbol as deprecated:</p>
  173. <p><strong>Note</strong>: <a class="reference external" href="https://github.com/nim-lang/c2nim/blob/master/doc/c2nim.rst">c2nim</a> can parse a large subset of C++ and knows about the <tt class="docutils literal"><span class="pre">importcpp</span></tt> pragma pattern language. It is not necessary to know all the details described here.</p>
  174. <p>Pure libraries do not depend on any external <tt class="docutils literal"><span class="pre">*.dll</span></tt> or <tt class="docutils literal"><span class="pre">lib*.so</span></tt> binary while impure libraries do. A wrapper is an impure library that is a very low-level interface to a C library.</p>
  175. <h1><a class="toc-backref" id="pure-libraries" href="#pure-libraries">Pure libraries</a></h1>
  176. <h2><a class="toc-backref" id="pure-libraries-automatic-imports" href="#pure-libraries-automatic-imports">Automatic imports</a></h2><ul class="simple"><li><a class="reference external" href="system.html">system</a> Basic procs and operators that every program needs. It also provides IO facilities for reading and writing text and binary files. It is imported implicitly by the compiler. Do not import it directly. It relies on compiler magic to work.</li>
  177. <li><a class="reference external" href="threads.html">threads</a> Basic Nim thread support. <strong>Note</strong>: This is part of the system module. Do not import it explicitly. Enabled with <tt class="docutils literal"><span class="pre">--threads:on</span></tt>.</li>
  178. </ul>
  179. <h1><a class="toc-backref" id="code-reordering" href="#code-reordering">Code reordering</a></h1><p>The code reordering feature can implicitly rearrange procedure, template, and macro definitions along with variable declarations and initializations at the top level scope so that, to a large extent, a programmer should not have to worry about ordering definitions correctly or be forced to use forward declarations to preface definitions inside a module.</p>
  180. <p>Example:</p>
  181. <pre class="listing"><span class="Punctuation">{</span><span class="Operator">.</span><span class="Identifier">experimental</span><span class="Punctuation">:</span> <span class="StringLit">&quot;codeReordering&quot;</span><span class="Operator">.</span><span class="Punctuation">}</span>
  182. <span class="Keyword">proc</span> <span class="Identifier">foo</span><span class="Punctuation">(</span><span class="Identifier">x</span><span class="Punctuation">:</span> <span class="Identifier">int</span><span class="Punctuation">)</span> <span class="Operator">=</span>
  183. <span class="Identifier">bar</span><span class="Punctuation">(</span><span class="Identifier">x</span><span class="Punctuation">)</span>
  184. <span class="Keyword">proc</span> <span class="Identifier">bar</span><span class="Punctuation">(</span><span class="Identifier">x</span><span class="Punctuation">:</span> <span class="Identifier">int</span><span class="Punctuation">)</span> <span class="Operator">=</span>
  185. <span class="Identifier">echo</span><span class="Punctuation">(</span><span class="Identifier">x</span><span class="Punctuation">)</span>
  186. <span class="Identifier">foo</span><span class="Punctuation">(</span><span class="DecNumber">10</span><span class="Punctuation">)</span></pre><p>It is important to note that reordering <em>only</em> works for symbols at top level scope. Therefore, the following will <em>fail to compile:</em></p>
  187. <h2><a class="toc-backref" id="code-reordering-parameter-constraints" href="#code-reordering-parameter-constraints">Parameter constraints</a></h2><p>The <span id="parameter-constraint_1">parameter constraint</span> expression can use the operators <tt class="docutils literal"><span class="pre">|</span></tt> (or), <tt class="docutils literal"><span class="pre">&amp;</span></tt> (and) and <tt class="docutils literal"><span class="pre">~</span></tt> (not) and the following predicates:</p>
  188. <h3><a class="toc-backref" id="parameter-constraints-the-tilde-operator" href="#parameter-constraints-the-tilde-operator">The <tt class="docutils literal"><span class="pre">~</span></tt> operator</a></h3><p>The <tt class="docutils literal"><span class="pre">~</span></tt> operator is the <strong>not</strong> operator in patterns:</p>
  189. <h3><a class="toc-backref" id="parameter-constraints-the-starstar-operator" href="#parameter-constraints-the-starstar-operator">The <tt class="docutils literal"><span class="pre">**</span></tt> operator</a></h3><p>The <tt class="docutils literal"><span class="pre">**</span></tt> is much like the <tt class="docutils literal"><span class="pre">*</span></tt> operator, except that it gathers not only all the arguments, but also the matched operators in reverse polish notation:</p>
  190. <p>Nim significantly improves on the safety of these features via additional pragmas:</p>
  191. <ol class="simple"><li>A <span id="guard_1">guard</span> annotation is introduced to prevent data races.</li>
  192. <li>Every access of a guarded memory location needs to happen in an appropriate <span id="locks_1">locks</span> statement.</li>
  193. <li>Locks and routines can be annotated with <span id="lock-levels_1">lock levels</span> to allow potential deadlocks to be detected during semantic analysis.</li>
  194. </ol>
  195. <ol class="simple"><li>Two output parameters should never be aliased.</li>
  196. <li>An input and an output parameter should not be aliased.</li>
  197. <li>An output parameter should never be aliased with a global or thread local variable referenced by the called proc.</li>
  198. <li>An input parameter should not be aliased with a global or thread local variable updated by the called proc.</li>
  199. </ol>
  200. <p>One problem with rules 3 and 4 is that they affect specific global or thread local variables, but Nim's effect tracking only tracks &quot;uses no global variable&quot; via <tt class="docutils literal"><span class="pre">.noSideEffect</span></tt>. The rules 3 and 4 can also be approximated by a different rule:</p>
  201. <ol class="simple" start="5"><li>A global or thread local variable (or a location derived from such a location) can only passed to a parameter of a <tt class="docutils literal"><span class="pre">.noSideEffect</span></tt> proc.</li>
  202. </ol>
  203. <p>These two procs are the two modus operandi of the real-time garbage collector:</p>
  204. <ol class="simple"><li><p>GC_SetMaxPause Mode</p>
  205. <p>You can call <tt class="docutils literal"><span class="pre">GC_SetMaxPause</span></tt> at program startup and then each triggered garbage collector run tries to not take longer than <tt class="docutils literal"><span class="pre">maxPause</span></tt> time. However, it is possible (and common) that the work is nevertheless not evenly distributed as each call to <tt class="docutils literal"><span class="pre">new</span></tt> can trigger the garbage collector and thus take <tt class="docutils literal"><span class="pre">maxPause</span></tt> time.</p>
  206. </li>
  207. <li><p>GC_step Mode</p>
  208. <p>This allows the garbage collector to perform some work for up to <tt class="docutils literal"><span class="pre">us</span></tt> time. This is useful to call in the main loop to ensure the garbage collector can do its work. To bind all garbage collector activity to a <tt class="docutils literal"><span class="pre">GC_step</span></tt> call, deactivate the garbage collector with <tt class="docutils literal"><span class="pre">GC_disable</span></tt> at program startup. If <tt class="docutils literal"><span class="pre">strongAdvice</span></tt> is set to <tt class="docutils literal"><span class="pre">true</span></tt>, then the garbage collector will be forced to perform the collection cycle. Otherwise, the garbage collector may decide not to do anything, if there is not much garbage to collect. You may also specify the current stack size via <tt class="docutils literal"><span class="pre">stackSize</span></tt> parameter. It can improve performance when you know that there are no unique Nim references below a certain point on the stack. Make sure the size you specify is greater than the potential worst-case size.</p>
  209. <p>It can improve performance when you know that there are no unique Nim references below a certain point on the stack. Make sure the size you specify is greater than the potential worst-case size.</p>
  210. </li>
  211. </ol>
  212. <p>These procs provide a &quot;best effort&quot; real-time guarantee; in particular the cycle collector is not aware of deadlines. Deactivate it to get more predictable real-time behaviour. Tests show that a 1ms max pause time will be met in almost all cases on modern CPUs (with the cycle collector disabled).</p>
  213. <h2><a class="toc-backref" id="code-reordering-time-measurement-with-garbage-collectors" href="#code-reordering-time-measurement-with-garbage-collectors">Time measurement with garbage collectors</a></h2><p>The garbage collectors' way of measuring time uses (see <tt class="docutils literal"><span class="pre">lib/system/timers.nim</span></tt> for the implementation):</p>
  214. <ol class="simple"><li><tt class="docutils literal"><span class="pre">QueryPerformanceCounter</span></tt> and <tt class="docutils literal"><span class="pre">QueryPerformanceFrequency</span></tt> on Windows.</li>
  215. <li><tt class="docutils literal"><span class="pre">mach_absolute_time</span></tt> on Mac OS X.</li>
  216. <li><tt class="docutils literal"><span class="pre">gettimeofday</span></tt> on Posix systems.</li>
  217. </ol>
  218. <p>As such it supports a resolution of nanoseconds internally; however, the API uses microseconds for convenience.</p>
  219. <h1><a class="toc-backref" id="introduction" href="#introduction">Introduction</a></h1><blockquote><p>
  220. "Der Mensch ist doch ein Augentier -- sch&ouml;ne Dinge w&uuml;nsch ich mir."
  221. </p></blockquote><p>This document is a tutorial for the programming language <em>Nim</em>. This tutorial assumes that you are familiar with basic programming concepts like variables, types, or statements but is kept very basic. The <a class="reference external" href="manual.html">manual</a> contains many more examples of the advanced language features. All code examples in this tutorial, as well as the ones found in the rest of Nim's documentation, follow the <a class="reference external" href="nep1.html">Nim style guide</a>.</p>
  222. <p>However, this does not work. The problem is that the procedure should not only <tt class="docutils literal"><span class="pre">return</span></tt>, but return and <strong>continue</strong> after an iteration has finished. This <em>return and continue</em> is called a <tt class="docutils literal"><span class="pre"><span class="Keyword">yield</span></span></tt> statement. Now the only thing left to do is to replace the <tt class="docutils literal"><span class="pre">proc</span></tt> keyword by <tt class="docutils literal"><span class="pre">iterator</span></tt> and here it is - our first iterator:</p>
  223. <table border="1" class="docutils"><tr><th>A1 header</th><th>A2 | not fooled</th></tr>
  224. <tr><td>C1</td><td>C2 <strong>bold</strong></td></tr>
  225. <tr><td>D1 <tt class="docutils literal"><span class="pre"><span class="Identifier">code</span> <span class="Operator">\|</span></span></tt></td><td>D2</td></tr>
  226. <tr><td>E1 | text</td><td></td></tr>
  227. <tr><td></td><td>F2 without pipe</td></tr>
  228. </table><p>not in table </p>
  229. </p>
  230. </div>
  231. </div>
  232. <div class="twelve-columns footer">
  233. <span class="nim-sprite"></span>
  234. <br>
  235. <small style="color: var(--hint);">Made with Nim. Generated: 1970-01-02 03:46:40 UTC</small>
  236. </div>
  237. </div>
  238. </div>
  239. <!-- Google fonts -->
  240. <link href='https://fonts.googleapis.com/css?family=Lato:400,600,900' rel='stylesheet' type='text/css'/>
  241. <link href='https://fonts.googleapis.com/css?family=Source+Code+Pro:400,500,600' rel='stylesheet' type='text/css'/>
  242. </body>
  243. </html>