-
Notifications
You must be signed in to change notification settings - Fork 1
/
manual.html
921 lines (870 loc) · 92.6 KB
/
manual.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
<!DOCTYPE html>
<html lang="en" data-content_root="./">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" /><meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Manual — Neat documentation</title>
<link rel="stylesheet" type="text/css" href="_static/pygments.css?v=4f649999" />
<link rel="stylesheet" type="text/css" href="_static/alabaster.css?v=039e1c02" />
<script src="_static/jquery.js?v=5d32c60e"></script>
<script src="_static/_sphinx_javascript_frameworks_compat.js?v=2cd50e6c"></script>
<script src="_static/documentation_options.js?v=5929fcd5"></script>
<script src="_static/doctools.js?v=888ff710"></script>
<script src="_static/sphinx_highlight.js?v=dc90522c"></script>
<link rel="index" title="Index" href="genindex.html" />
<link rel="search" title="Search" href="search.html" />
<link rel="next" title="Standard Library" href="std.html" />
<link rel="prev" title="Introduction" href="intro.html" />
<link rel="stylesheet" href="_static/custom.css" type="text/css" />
<meta name="viewport" content="width=device-width, initial-scale=0.9, maximum-scale=0.9" />
</head><body>
<div class="document">
<div class="documentwrapper">
<div class="bodywrapper">
<div class="body" role="main">
<section id="id1">
<h1>Manual<a class="headerlink" href="#id1" title="Link to this heading">¶</a></h1>
<p>This document goes through every Neat language feature in approximate sequence.
The goal is that you should be able to understand the entire language by just
reading it top to bottom. But you can also use it as a reference to
quickly find how a feature works.</p>
<section id="lexical">
<h2>Lexical<a class="headerlink" href="#lexical" title="Link to this heading">¶</a></h2>
<p>All Neat source files are encoded as UTF-8.</p>
<p>Comment syntax is as in C:</p>
<div class="highlight-d notranslate"><div class="highlight"><pre><span></span><span class="cm">/**</span>
<span class="cm"> * This is a comment. It goes from /* to */</span><span class="p">.</span>
<span class="w"> </span><span class="p">*/</span>
<span class="c1">// This is also a comment. It goes to the end of the line.</span>
<span class="cm">/* Comments can be /* nested. */</span><span class="w"> </span><span class="p">*/</span>
</pre></div>
</div>
<p>Comments may appear anywhere in a source file except inside identifiers or operators.</p>
<p>An identifier is a letter or underscore, followed by a sequence of letters, digits or underscores.</p>
</section>
<section id="modules">
<h2>Modules<a class="headerlink" href="#modules" title="Link to this heading">¶</a></h2>
<p>A Neat source file has the extension <code class="code docutils literal notranslate"><span class="pre">.nt</span></code>. Each source file corresponds to a module.
A module is a dot-separated list of packages, corresponding to folders, followed the filename.</p>
<p>Every file must begin with the module declaration. For instance, a file <code class="code docutils literal notranslate"><span class="pre">src/hello/world.nt</span></code>:</p>
<div class="highlight-d notranslate"><div class="highlight"><pre><span></span><span class="k">module</span><span class="w"> </span><span class="n">hello</span><span class="p">.</span><span class="n">world</span><span class="p">;</span>
</pre></div>
</div>
</section>
<section id="packages">
<h2>Packages<a class="headerlink" href="#packages" title="Link to this heading">¶</a></h2>
<p>Neat does not use includes, but instead packages. A package is a folder associated with a name:</p>
<div class="highlight-none notranslate"><div class="highlight"><pre><span></span>$ # -P<name>:<folder>[:<dependency>[,<dependency]*]
$ neat -Proot:src src/hello/world.nt
</pre></div>
</div>
<p>This defines the folder <code class="code docutils literal notranslate"><span class="pre">./src</span></code> to be the package <code class="code docutils literal notranslate"><span class="pre">root</span></code>. The file passed will be the module
<code class="code docutils literal notranslate"><span class="pre">hello.world</span></code>, because its name will be relative to <code class="code docutils literal notranslate"><span class="pre">src</span></code>.</p>
<p>Packages cannot access modules in other packages. To allow a package access, explicitly
list the packages that it has access to:</p>
<div class="highlight-d notranslate"><div class="highlight"><pre><span></span><span class="p">$</span><span class="w"> </span><span class="n">neat</span><span class="w"> </span><span class="p">-</span><span class="n">Proot</span><span class="p">:</span><span class="n">src</span><span class="p">:</span><span class="n">dep1</span><span class="p">,</span><span class="n">dep2</span><span class="w"> </span><span class="p">-</span><span class="n">Pdep1</span><span class="p">:</span><span class="n">include</span><span class="p">/</span><span class="n">dep1</span><span class="w"> </span><span class="p">-</span><span class="n">Pdep2</span><span class="p">:</span><span class="n">include</span><span class="p">/</span><span class="n">dep2</span>
</pre></div>
</div>
<p>This allows the modules from package <code class="code docutils literal notranslate"><span class="pre">root</span></code> in <code class="code docutils literal notranslate"><span class="pre">src/</span></code> to import the modules
in <code class="code docutils literal notranslate"><span class="pre">dep1</span></code> and <code class="code docutils literal notranslate"><span class="pre">dep2</span></code>.
Because dependencies are explicitly listed, accidental import of modules from
an unrelated package is impossible.</p>
</section>
<section id="module-level-statements">
<h2>Module-Level Statements<a class="headerlink" href="#module-level-statements" title="Link to this heading">¶</a></h2>
<section id="import">
<h3>Import<a class="headerlink" href="#import" title="Link to this heading">¶</a></h3>
<p>A module can import another module:</p>
<div class="highlight-d notranslate"><div class="highlight"><pre><span></span><span class="k">module</span><span class="w"> </span><span class="n">hello</span><span class="p">.</span><span class="n">world</span><span class="p">;</span>
<span class="k">import</span><span class="w"> </span><span class="n">std</span><span class="p">.</span><span class="n">stdio</span><span class="p">;</span>
</pre></div>
</div>
<p>Import is non-transitive, ie. symbols from modules imported by <code class="code docutils literal notranslate"><span class="pre">std.stdio</span></code> are invisible.</p>
<p>Modules can be imported transitively:</p>
<div class="highlight-d notranslate"><div class="highlight"><pre><span></span><span class="k">module</span><span class="w"> </span><span class="n">first</span><span class="p">;</span>
<span class="k">public</span><span class="w"> </span><span class="k">import</span><span class="w"> </span><span class="n">second</span><span class="p">;</span>
</pre></div>
</div>
<p>Now all modules that import <code class="code docutils literal notranslate"><span class="pre">first</span></code> will also see the symbols in <code class="code docutils literal notranslate"><span class="pre">second</span></code>.</p>
<p>Symbols can be imported by name: <code class="code docutils literal notranslate"><span class="pre">import</span> <span class="pre">std.stdio</span> <span class="pre">:</span> <span class="pre">print;</span></code>.</p>
</section>
<section id="declaration">
<h3>Declaration<a class="headerlink" href="#declaration" title="Link to this heading">¶</a></h3>
<p>Structs, classes, templates and functions can be declared by name.
Every declaration can be marked as <code class="code docutils literal notranslate"><span class="pre">public</span></code> or <code class="code docutils literal notranslate"><span class="pre">private</span></code>; they are <code class="code docutils literal notranslate"><span class="pre">public</span></code> by default.
Private declarations cannot be seen when the module is imported.</p>
</section>
<section id="extern-c">
<h3>Extern(C)<a class="headerlink" href="#extern-c" title="Link to this heading">¶</a></h3>
<p>A function can be declared as <code class="code docutils literal notranslate"><span class="pre">extern(C)</span></code>. This will ensure that it matches the calling convention of the platform’s native C compiler.</p>
<p>For example:</p>
<div class="highlight-d notranslate"><div class="highlight"><pre><span></span><span class="k">extern</span><span class="p">(</span><span class="n">C</span><span class="p">)</span><span class="w"> </span><span class="kt">void</span><span class="p">*</span><span class="w"> </span><span class="n">memcpy</span><span class="p">(</span><span class="kt">void</span><span class="p">*</span><span class="w"> </span><span class="n">dest</span><span class="p">,</span><span class="w"> </span><span class="kt">void</span><span class="p">*</span><span class="w"> </span><span class="n">src</span><span class="p">,</span><span class="w"> </span><span class="nb">size_t</span><span class="w"> </span><span class="n">n</span><span class="p">);</span>
</pre></div>
</div>
<p>Note: instead of declaring lots of extern(C) functions manually, try using the
<code class="code docutils literal notranslate"><span class="pre">std.macro.cimport</span></code> built-in macro! (Grep for examples.)</p>
</section>
</section>
<section id="expressions">
<h2>Expressions<a class="headerlink" href="#expressions" title="Link to this heading">¶</a></h2>
<section id="literals">
<h3>Literals<a class="headerlink" href="#literals" title="Link to this heading">¶</a></h3>
<p><code class="code docutils literal notranslate"><span class="pre">5</span></code> is an integer literal of type <code class="code docutils literal notranslate"><span class="pre">int</span></code>.</p>
<p>Integer literals may be arbitrarily divided by underscores for readability: <code class="code docutils literal notranslate"><span class="pre">1_048_576</span></code>.</p>
<p><code class="code docutils literal notranslate"><span class="pre">"Hello</span> <span class="pre">World"</span></code> is a string literal. <code class="code docutils literal notranslate"><span class="pre">string</span></code> is the same as <code class="code docutils literal notranslate"><span class="pre">char[]</span></code>.</p>
<p>You can interpolate values into a string literal with <code class="code docutils literal notranslate"><span class="pre">$name</span></code> or <code class="code docutils literal notranslate"><span class="pre">$(expression)</span></code>. The
compiler will select a type-appropriate default representation. For class and struct types,
<code class="code docutils literal notranslate"><span class="pre">toString</span></code> will be called. An example:</p>
<div class="highlight-d notranslate"><div class="highlight"><pre><span></span><span class="n">print</span><span class="p">(</span><span class="s">"$remaining bottles of beer on the wall."</span><span class="p">);</span>
</pre></div>
</div>
<p>As in Python, expressions with a trailing <code class="code docutils literal notranslate"><span class="pre">=</span></code> are quoted in the output:</p>
<div class="highlight-d notranslate"><div class="highlight"><pre><span></span><span class="kt">int</span><span class="w"> </span><span class="n">a</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="mi">2</span><span class="p">,</span><span class="w"> </span><span class="n">b</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="mi">3</span><span class="p">;</span>
<span class="n">print</span><span class="p">(</span><span class="s">"$(a + b =)"</span><span class="p">);</span><span class="w"> </span><span class="c1">// "a + b = 5"</span>
</pre></div>
</div>
<p><code class="code docutils literal notranslate"><span class="pre">`Hello</span> <span class="pre">World`</span></code> is a literal string. Unlike regular string literals,
escape sequences and format string quotes are not processed.</p>
<p>A backslash in a string starts an escape sequence.
These escape sequences are supported in strings:</p>
<ul class="simple">
<li><p><code class="code docutils literal notranslate"><span class="pre">\r</span></code>: carriage return</p></li>
<li><p><code class="code docutils literal notranslate"><span class="pre">\n</span></code>: newline</p></li>
<li><p><code class="code docutils literal notranslate"><span class="pre">\t</span></code>: tab</p></li>
<li><p><code class="code docutils literal notranslate"><span class="pre">\"</span></code>: double quote</p></li>
<li><p><code class="code docutils literal notranslate"><span class="pre">\'</span></code>: single quote</p></li>
<li><p><code class="code docutils literal notranslate"><span class="pre">\\</span></code>: backslash</p></li>
<li><p><code class="code docutils literal notranslate"><span class="pre">\0</span></code>: null character</p></li>
<li><p><code class="code docutils literal notranslate"><span class="pre">\x##</span></code>: two-digit hexadecimal character code (e.g., <code class="code docutils literal notranslate"><span class="pre">\x0A</span></code> for newline)</p></li>
<li><p><code class="code docutils literal notranslate"><span class="pre">\$</span></code>: dollar sign</p></li>
</ul>
<p>Additionally, as in Rust, a backslash followed by a newline character (<code class="code docutils literal notranslate"><span class="pre">\n</span></code> or <code class="code docutils literal notranslate"><span class="pre">\r\n</span></code>) indicates
a line continuation. The newline and all succeeding whitespace are removed. Note that as <em>opposed</em>
to Rust, newline characters beyond the first are not skipped!</p>
<p><code class="code docutils literal notranslate"><span class="pre">1.2</span></code> is a <code class="code docutils literal notranslate"><span class="pre">double</span></code> literal. <code class="code docutils literal notranslate"><span class="pre">1.2f</span></code> is a <code class="code docutils literal notranslate"><span class="pre">float</span></code> literal.</p>
</section>
<section id="arithmetic">
<h3>Arithmetic<a class="headerlink" href="#arithmetic" title="Link to this heading">¶</a></h3>
<p>Binary operations can be performed on types. These are:</p>
<table class="docutils align-default">
<thead>
<tr class="row-odd"><th class="head"><p>Operation</p></th>
<th class="head"><p>Description</p></th>
<th class="head"><p>Rank</p></th>
</tr>
</thead>
<tbody>
<tr class="row-even"><td><p><code class="code docutils literal notranslate"><span class="pre">a</span> <span class="pre">||</span> <span class="pre">b</span></code></p></td>
<td><p>Boolean “or”</p></td>
<td><p>1</p></td>
</tr>
<tr class="row-odd"><td><p><code class="code docutils literal notranslate"><span class="pre">a</span> <span class="pre">&&</span> <span class="pre">b</span></code></p></td>
<td><p>Boolean “and”</p></td>
<td><p>2</p></td>
</tr>
<tr class="row-even"><td><p><code class="code docutils literal notranslate"><span class="pre">a</span> <span class="pre"><=</span> <span class="pre">b</span></code></p></td>
<td><p>Comparison</p></td>
<td><p>3</p></td>
</tr>
<tr class="row-odd"><td><p><code class="code docutils literal notranslate"><span class="pre">a</span> <span class="pre">..</span> <span class="pre">b</span></code></p></td>
<td><p>Range</p></td>
<td><p>4</p></td>
</tr>
<tr class="row-even"><td><p><code class="code docutils literal notranslate"><span class="pre">a</span> <span class="pre">+</span> <span class="pre">b</span></code></p></td>
<td><p>Addition</p></td>
<td><p>5</p></td>
</tr>
<tr class="row-odd"><td><p><code class="code docutils literal notranslate"><span class="pre">a</span> <span class="pre">-</span> <span class="pre">b</span></code></p></td>
<td><p>Subtraction</p></td>
<td><p>5</p></td>
</tr>
<tr class="row-even"><td><p><code class="code docutils literal notranslate"><span class="pre">a</span> <span class="pre">~</span> <span class="pre">b</span></code></p></td>
<td><p>Concatenation</p></td>
<td><p>5</p></td>
</tr>
<tr class="row-odd"><td><p><code class="code docutils literal notranslate"><span class="pre">a</span> <span class="pre">*</span> <span class="pre">b</span></code></p></td>
<td><p>Multiplication</p></td>
<td><p>6</p></td>
</tr>
<tr class="row-even"><td><p><code class="code docutils literal notranslate"><span class="pre">a</span> <span class="pre">/</span> <span class="pre">b</span></code></p></td>
<td><p>Division</p></td>
<td><p>6</p></td>
</tr>
<tr class="row-odd"><td><p><code class="code docutils literal notranslate"><span class="pre">a</span> <span class="pre">|</span> <span class="pre">b</span></code></p></td>
<td><p>Bitwise “or”</p></td>
<td><p>7</p></td>
</tr>
<tr class="row-even"><td><p><code class="code docutils literal notranslate"><span class="pre">a</span> <span class="pre"><<</span> <span class="pre">b</span></code></p></td>
<td><p>Left shift</p></td>
<td><p>8</p></td>
</tr>
<tr class="row-odd"><td><p><code class="code docutils literal notranslate"><span class="pre">a</span> <span class="pre">>></span> <span class="pre">b</span></code></p></td>
<td><p>Right shift</p></td>
<td><p>8</p></td>
</tr>
<tr class="row-even"><td><p><code class="code docutils literal notranslate"><span class="pre">a</span> <span class="pre">^</span> <span class="pre">b</span></code></p></td>
<td><p>Bitwise “xor”</p></td>
<td><p>9</p></td>
</tr>
<tr class="row-odd"><td><p><code class="code docutils literal notranslate"><span class="pre">a</span> <span class="pre">&</span> <span class="pre">b</span></code></p></td>
<td><p>Bitwise “and”</p></td>
<td><p>10</p></td>
</tr>
</tbody>
</table>
<p>Boolean “or” and “and” are short-circuiting. Comparison operators are <code class="code docutils literal notranslate"><span class="pre">></span></code>, <code class="code docutils literal notranslate"><span class="pre">==</span></code>, <code class="code docutils literal notranslate"><span class="pre"><</span></code>, <code class="code docutils literal notranslate"><span class="pre">>=</span></code>, <code class="code docutils literal notranslate"><span class="pre"><=</span></code>, and <code class="code docutils literal notranslate"><span class="pre">!=</span></code>.
Higher-ranked operators take precedence over lower-ranked, with boolean operators being the loosest.</p>
<p>Note that the placement of bitwise operators diverges from C’s order.
This is because C’s order is stupid^W a legacy holdover from before it had boolean operators.</p>
<p>Operator precedence can be clarified using parentheses: <code class="code docutils literal notranslate"><span class="pre">2</span> <span class="pre">*</span> <span class="pre">(3</span> <span class="pre">+</span> <span class="pre">4)</span></code> instead of <code class="code docutils literal notranslate"><span class="pre">2</span> <span class="pre">*</span> <span class="pre">3</span> <span class="pre">+</span> <span class="pre">4</span></code>.</p>
</section>
<section id="ternary-if">
<h3>Ternary If<a class="headerlink" href="#ternary-if" title="Link to this heading">¶</a></h3>
<p><code class="code docutils literal notranslate"><span class="pre">a</span> <span class="pre">if</span> <span class="pre">t</span> <span class="pre">else</span> <span class="pre">b</span></code> has the value of <code class="code docutils literal notranslate"><span class="pre">a</span></code> if <code class="code docutils literal notranslate"><span class="pre">t</span></code> is true, else it has the value of <code class="code docutils literal notranslate"><span class="pre">b</span></code>.</p>
<p>Only the selected expression is evaluated. So if <code class="code docutils literal notranslate"><span class="pre">t</span></code> is true, <code class="code docutils literal notranslate"><span class="pre">b</span></code> is never evaluated.</p>
<p>This operator has a lower rank than any of the binary operators.</p>
<p>The ternary operator can be shortened to <code class="code docutils literal notranslate"><span class="pre">a</span> <span class="pre">else</span> <span class="pre">b</span></code>. In that case, <code class="code docutils literal notranslate"><span class="pre">a</span></code> is always taken unless
the expression branches to <code class="code docutils literal notranslate"><span class="pre">b</span></code> via <code class="code docutils literal notranslate"><span class="pre">breakelse</span></code>.</p>
<p>The ternary operator syntax diverges from C because <code class="code docutils literal notranslate"><span class="pre">?</span></code> is already used for error propagation.</p>
</section>
<section id="control-flow-expressions">
<h3>Control flow expressions<a class="headerlink" href="#control-flow-expressions" title="Link to this heading">¶</a></h3>
<p><code class="code docutils literal notranslate"><span class="pre">break</span></code> is an expression that, when evaluated, transfers control flow to after the current loop.</p>
<p><code class="code docutils literal notranslate"><span class="pre">continue</span></code> is an expression that, when evaluated, transfers control flow to the next pass
of the current loop.</p>
<p><code class="code docutils literal notranslate"><span class="pre">return</span></code>, or <code class="code docutils literal notranslate"><span class="pre">return</span> <span class="pre">x</span></code>, is an expression that, when evaluated, transfers control flow out of
the current function. If a parameter <code class="code docutils literal notranslate"><span class="pre">x</span></code> is given, the current function call evaluates to
<code class="code docutils literal notranslate"><span class="pre">x</span></code>; else it evaluates to <code class="code docutils literal notranslate"><span class="pre">void</span></code>.</p>
<p><code class="code docutils literal notranslate"><span class="pre">breakelse</span></code> is an expression that, when evaluated, transfers control flow to the <code class="code docutils literal notranslate"><span class="pre">else</span></code> block
of the surrounding <code class="code docutils literal notranslate"><span class="pre">if</span></code> statement, or causes the <code class="code docutils literal notranslate"><span class="pre">else</span></code> expression of the surrounding ternary <code class="code docutils literal notranslate"><span class="pre">if</span></code>
to be used. If the <code class="code docutils literal notranslate"><span class="pre">if</span></code> statement has no <code class="code docutils literal notranslate"><span class="pre">else</span></code> block, it continues after the <code class="code docutils literal notranslate"><span class="pre">if</span></code> block.</p>
<p>Since these expressions exit the local scope (they’re “non-local control flow primitives”),
they are all typed <code class="code docutils literal notranslate"><span class="pre">bottom</span></code> - their <em>local</em> value is empty.</p>
</section>
<section id="error-propagation-operator">
<h3>Error propagation operator<a class="headerlink" href="#error-propagation-operator" title="Link to this heading">¶</a></h3>
<p><code class="code docutils literal notranslate"><span class="pre">x?</span></code> is the error propagation operator. Its behavior depends on the type of <code class="code docutils literal notranslate"><span class="pre">x</span></code>:</p>
<ul class="simple">
<li><p>if <code class="code docutils literal notranslate"><span class="pre">x</span></code> is a subtype of <code class="code docutils literal notranslate"><span class="pre">std.error.Error</span></code>, it is returned from the current function.</p></li>
<li><dl class="simple">
<dt>if <code class="code docutils literal notranslate"><span class="pre">x</span></code> is a sumtype:</dt><dd><ul>
<li><p>all subclasses of <code class="code docutils literal notranslate"><span class="pre">std.error.Error</span></code> are returned from the current function.</p></li>
<li><p>all types marked <code class="code docutils literal notranslate"><span class="pre">fail</span></code> are returned from the current function.
This is a legacy feature: <code class="code docutils literal notranslate"><span class="pre">Error</span></code> subclasses should be preferred.</p></li>
<li><p>if it contains an <code class="code docutils literal notranslate"><span class="pre">:else</span></code> type, it is mapped to <code class="code docutils literal notranslate"><span class="pre">breakelse</span></code>.</p></li>
<li><p>if it contains a <code class="code docutils literal notranslate"><span class="pre">nullptr_t</span></code> type, it is mapped to <code class="code docutils literal notranslate"><span class="pre">breakelse</span></code>.</p></li>
</ul>
</dd>
</dl>
</li>
<li><p>if <code class="code docutils literal notranslate"><span class="pre">x</span></code> is a <code class="code docutils literal notranslate"><span class="pre">nullable</span> <span class="pre">T</span></code>, it is treated as a sumtype of <code class="code docutils literal notranslate"><span class="pre">T</span> <span class="pre">|</span> <span class="pre">nullptr_t</span></code>.
The <code class="code docutils literal notranslate"><span class="pre">nullptr_t</span></code> is then mapped to <code class="code docutils literal notranslate"><span class="pre">breakelse</span></code>.</p></li>
</ul>
<p>The member types <code class="code docutils literal notranslate"><span class="pre">nullptr_t</span></code> and <code class="code docutils literal notranslate"><span class="pre">:else</span></code> are thus interpreted as “not error, not success”:
they are “expected failures” that exit the current <code class="code docutils literal notranslate"><span class="pre">if</span></code> test but not the function.
For instance, when reading data from a file, an I/O error class would subclass <code class="code docutils literal notranslate"><span class="pre">Error</span></code> and
thus be returned, but reaching the end of the file would be communicated by <code class="code docutils literal notranslate"><span class="pre">:else</span></code>.</p>
<p>Since <code class="code docutils literal notranslate"><span class="pre">?</span></code> maps certain types to control flow expressions, which are typed <code class="code docutils literal notranslate"><span class="pre">bottom</span></code>,
they are removed from the sumtype. As such, <code class="code docutils literal notranslate"><span class="pre">?</span></code> leaves only successful types behind.</p>
<p>Note that when a sumtype contains both <code class="code docutils literal notranslate"><span class="pre">Error</span></code>/<code class="code docutils literal notranslate"><span class="pre">fail</span></code> types <em>and</em> a nullable class,
the first application of <code class="code docutils literal notranslate"><span class="pre">?</span></code> will only get rid of the <code class="code docutils literal notranslate"><span class="pre">Error</span></code>/<code class="code docutils literal notranslate"><span class="pre">fail</span></code> types: you may require two <code class="code docutils literal notranslate"><span class="pre">?</span></code>.</p>
<p>Example:</p>
<div class="highlight-d notranslate"><div class="highlight"><pre><span></span><span class="nb">string</span><span class="w"> </span><span class="n">line</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="n">file</span><span class="p">.</span><span class="n">readText</span><span class="p">()??</span><span class="w"> </span><span class="k">else</span><span class="w"> </span><span class="n">die</span><span class="p">;</span>
<span class="n">nullable</span><span class="w"> </span><span class="n">Class</span><span class="w"> </span><span class="n">obj</span><span class="p">;</span>
<span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="k">auto</span><span class="w"> </span><span class="n">var</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="n">obj</span><span class="p">?.</span><span class="n">field</span><span class="p">?)</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="p">}</span>
<span class="k">while</span><span class="w"> </span><span class="p">(</span><span class="kc">true</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
<span class="w"> </span><span class="k">auto</span><span class="w"> </span><span class="n">data</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="n">file</span><span class="p">.</span><span class="n">readBlock</span><span class="p">?</span><span class="w"> </span><span class="k">else</span><span class="w"> </span><span class="k">break</span><span class="p">;</span>
<span class="w"> </span><span class="p">...</span>
<span class="p">}</span>
</pre></div>
</div>
</section>
</section>
<section id="functions">
<h2>Functions<a class="headerlink" href="#functions" title="Link to this heading">¶</a></h2>
<p>A function is a series of statements operating on a list of parameters, culminating in a return value:</p>
<div class="highlight-d notranslate"><div class="highlight"><pre><span></span><span class="n">ReturnType</span><span class="w"> </span><span class="n">functionName</span><span class="p">(</span><span class="n">ParameterType</span><span class="w"> </span><span class="n">parameterName</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
<span class="w"> </span><span class="n">statement</span><span class="p">;</span>
<span class="w"> </span><span class="n">statement</span><span class="p">;</span>
<span class="w"> </span><span class="n">statement</span><span class="p">;</span>
<span class="w"> </span><span class="k">return</span><span class="w"> </span><span class="mi">5</span><span class="p">;</span>
<span class="p">}</span>
<span class="p">...</span>
<span class="w"> </span><span class="n">ReturnType</span><span class="w"> </span><span class="n">ret</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="n">functionName</span><span class="p">(</span><span class="n">foo</span><span class="p">);</span>
</pre></div>
</div>
<p>When a function is called with <code class="code docutils literal notranslate"><span class="pre">name(arg,</span> <span class="pre">arg)</span></code>, the arguments are passed to the parameters and
control passes to the function. The statements of the function are then executed, until control
returns to the caller when the function exits, by explicit <code class="code docutils literal notranslate"><span class="pre">return</span></code> or reaching its end.</p>
<p>If the return type is <code class="code docutils literal notranslate"><span class="pre">auto</span></code>, it is inferred from the type returned by the <code class="code docutils literal notranslate"><span class="pre">return</span></code> statements
in the function body. This is called return type inference.</p>
<section id="call">
<h3>Call<a class="headerlink" href="#call" title="Link to this heading">¶</a></h3>
<p>A function, class method or struct method can be called with a comma-separated list of arguments:</p>
<div class="highlight-d notranslate"><div class="highlight"><pre><span></span><span class="n">print</span><span class="p">(</span><span class="s">"Hello World"</span><span class="p">);</span>
<span class="kt">double</span><span class="w"> </span><span class="n">d</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="n">sin</span><span class="p">(</span><span class="mf">0.0</span><span class="p">);</span>
<span class="k">class</span><span class="p">.</span><span class="n">method</span><span class="p">();</span>
</pre></div>
</div>
<p>When a function does not have any parameters, the empty parens can be left out, and the function will be
called implicitly:</p>
<div class="highlight-d notranslate"><div class="highlight"><pre><span></span><span class="n">doWork</span><span class="p">;</span>
</pre></div>
</div>
<p>This also allows struct or class methods that look like properties.</p>
<section id="uniform-function-call-syntax">
<h4>Uniform Function Call Syntax<a class="headerlink" href="#uniform-function-call-syntax" title="Link to this heading">¶</a></h4>
<p>As in D, “uniform function call syntax” (UFCS) may be used. That is, if a call of the form <code class="code docutils literal notranslate"><span class="pre">a.method(b)</span></code>
did not find a method <code class="code docutils literal notranslate"><span class="pre">a.method</span></code> to call, it will instead be interpreted as <code class="code docutils literal notranslate"><span class="pre">method(a,</span> <span class="pre">b)</span></code>.
This allows easily defining global functions that can be called as if they are member functions of <code class="code docutils literal notranslate"><span class="pre">a</span></code>.</p>
</section>
<section id="named-arguments">
<h4>Named Arguments<a class="headerlink" href="#named-arguments" title="Link to this heading">¶</a></h4>
<p>The value of every parameter on a call may be assigned by name:</p>
<div class="highlight-d notranslate"><div class="highlight"><pre><span></span><span class="kt">int</span><span class="w"> </span><span class="n">twice</span><span class="p">(</span><span class="kt">int</span><span class="w"> </span><span class="n">x</span><span class="p">)</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="k">return</span><span class="w"> </span><span class="n">x</span><span class="w"> </span><span class="p">+</span><span class="w"> </span><span class="n">x</span><span class="p">;</span><span class="w"> </span><span class="p">}</span>
<span class="k">assert</span><span class="p">(</span><span class="n">twice</span><span class="p">(</span><span class="n">x</span><span class="p">=</span><span class="mi">2</span><span class="p">)</span><span class="w"> </span><span class="p">==</span><span class="w"> </span><span class="mi">4</span><span class="p">);</span>
</pre></div>
</div>
<p>This feature does not allow reordering parameters! It is purely intended to improve call readability, and to
ensure that arguments are passed to the intended parameter.</p>
</section>
</section>
<section id="nested-functions">
<h3>Nested functions<a class="headerlink" href="#nested-functions" title="Link to this heading">¶</a></h3>
<p>Functions may be nested inside other functions. They remain valid while the surrounding function is running,
and can access variables and parameters of the containing function, that were declared before them:</p>
<div class="highlight-d notranslate"><div class="highlight"><pre><span></span><span class="kt">int</span><span class="w"> </span><span class="kt">double</span><span class="p">(</span><span class="kt">int</span><span class="w"> </span><span class="n">a</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
<span class="w"> </span><span class="kt">int</span><span class="w"> </span><span class="n">add</span><span class="p">(</span><span class="kt">int</span><span class="w"> </span><span class="n">b</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
<span class="w"> </span><span class="k">return</span><span class="w"> </span><span class="n">a</span><span class="w"> </span><span class="p">+</span><span class="w"> </span><span class="n">b</span><span class="p">;</span>
<span class="w"> </span><span class="p">}</span>
<span class="w"> </span><span class="k">return</span><span class="w"> </span><span class="n">add</span><span class="p">(</span><span class="n">a</span><span class="p">);</span>
<span class="p">}</span>
</pre></div>
</div>
<p>Note that calling the nested function after the surrounding function has returned will lead to a crash!</p>
</section>
<section id="main">
<h3>main<a class="headerlink" href="#main" title="Link to this heading">¶</a></h3>
<p>Every program must contain a function with this signature:</p>
<div class="highlight-d notranslate"><div class="highlight"><pre><span></span><span class="kt">void</span><span class="w"> </span><span class="n">main</span><span class="p">(</span><span class="nb">string</span><span class="p">[]</span><span class="w"> </span><span class="n">args</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
<span class="p">}</span>
</pre></div>
</div>
<p>This function will be called when the program is executed.</p>
</section>
</section>
<section id="statements">
<h2>Statements<a class="headerlink" href="#statements" title="Link to this heading">¶</a></h2>
<section id="variable-declaration">
<h3>Variable declaration<a class="headerlink" href="#variable-declaration" title="Link to this heading">¶</a></h3>
<p>A variable can be declared like so:</p>
<div class="highlight-d notranslate"><div class="highlight"><pre><span></span><span class="kt">int</span><span class="w"> </span><span class="n">a</span><span class="p">;</span><span class="w"> </span><span class="c1">// a is 0</span>
<span class="kt">int</span><span class="w"> </span><span class="n">b</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="mi">5</span><span class="p">;</span>
<span class="kt">int</span><span class="w"> </span><span class="n">c</span><span class="p">,</span><span class="w"> </span><span class="n">d</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="mi">6</span><span class="p">;</span><span class="w"> </span><span class="c1">// c is 0</span>
<span class="n">mut</span><span class="w"> </span><span class="kt">int</span><span class="w"> </span><span class="n">e</span><span class="p">;</span>
</pre></div>
</div>
<p>Instead of a type, you may write <code class="code docutils literal notranslate"><span class="pre">auto</span></code>:</p>
<div class="highlight-d notranslate"><div class="highlight"><pre><span></span><span class="k">auto</span><span class="w"> </span><span class="n">f</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="mi">7</span><span class="p">;</span>
</pre></div>
</div>
<p>Then the type of the variable is taken from the type of the initializer.</p>
<p>Only mutable variables (<code class="code docutils literal notranslate"><span class="pre">mut</span> <span class="pre">a;</span></code>) may be changed later.</p>
</section>
<section id="variable-extraction-declaration">
<h3>Variable extraction declaration<a class="headerlink" href="#variable-extraction-declaration" title="Link to this heading">¶</a></h3>
<p>When an expression is a sumtype, a subset or a single type may be extracted as such:</p>
<div class="highlight-d notranslate"><div class="highlight"><pre><span></span><span class="p">(</span><span class="kt">int</span><span class="w"> </span><span class="p">|</span><span class="w"> </span><span class="n">Error</span><span class="p">)</span><span class="w"> </span><span class="n">foo</span><span class="p">;</span>
<span class="c1">// `Error` will be returned if `foo` is not `int`.</span>
<span class="kt">int</span><span class="w"> </span><span class="n">bar</span><span class="w"> </span><span class="p"><-</span><span class="w"> </span><span class="n">foo</span><span class="p">;</span>
</pre></div>
</div>
<div class="admonition note">
<p class="admonition-title">Note</p>
<p>This syntax is disabled pending renovations!
The new error propagation syntax <code class="code docutils literal notranslate"><span class="pre">foo?.bar</span></code> has made it superfluous.</p>
</div>
</section>
<section id="block-statement">
<h3>Block statement<a class="headerlink" href="#block-statement" title="Link to this heading">¶</a></h3>
<p>Multiple statements can be combined into one block:</p>
<div class="highlight-d notranslate"><div class="highlight"><pre><span></span><span class="p">{</span>
<span class="w"> </span><span class="n">print</span><span class="p">(</span><span class="s">"Hello"</span><span class="p">);</span>
<span class="w"> </span><span class="n">print</span><span class="p">(</span><span class="s">"World"</span><span class="p">);</span>
<span class="p">}</span>
</pre></div>
</div>
<p>Variables declared inside the block are not visible outside of it.</p>
</section>
<section id="expression-statement">
<h3>Expression statement<a class="headerlink" href="#expression-statement" title="Link to this heading">¶</a></h3>
<p>Expressions can appear as statements. They are terminated with a semicolon:</p>
<div class="highlight-d notranslate"><div class="highlight"><pre><span></span><span class="mi">5</span><span class="p">;</span>
<span class="n">foo</span><span class="p">();</span>
</pre></div>
</div>
</section>
<section id="assignment">
<h3>Assignment<a class="headerlink" href="#assignment" title="Link to this heading">¶</a></h3>
<p>Any reference may be assigned a new value:</p>
<div class="highlight-d notranslate"><div class="highlight"><pre><span></span><span class="n">mut</span><span class="w"> </span><span class="kt">int</span><span class="w"> </span><span class="n">a</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="mi">3</span><span class="p">;</span>
<span class="n">a</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="mi">5</span><span class="p">;</span>
<span class="k">assert</span><span class="p">(</span><span class="n">a</span><span class="w"> </span><span class="p">==</span><span class="w"> </span><span class="mi">5</span><span class="p">);</span>
</pre></div>
</div>
<p>Note that only mutable (<code class="code docutils literal notranslate"><span class="pre">mut</span></code>) variables or parameters can be reassigned. As this allows
some optimizations to reference counting, non-mutable variables should be preferred.</p>
</section>
<section id="if-block">
<h3>If block<a class="headerlink" href="#if-block" title="Link to this heading">¶</a></h3>
<p>If a condition is true, execute one statement, else the other:</p>
<div class="highlight-d notranslate"><div class="highlight"><pre><span></span><span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="mi">2</span><span class="w"> </span><span class="p">+</span><span class="w"> </span><span class="mi">2</span><span class="w"> </span><span class="p">==</span><span class="w"> </span><span class="mi">4</span><span class="p">)</span>
<span class="w"> </span><span class="n">print</span><span class="p">(</span><span class="s">"2 + 2 = 4"</span><span class="p">);</span>
<span class="k">else</span><span class="w"> </span><span class="p">{</span>
<span class="w"> </span><span class="n">print</span><span class="p">(</span><span class="s">"sanity has deserted us"</span><span class="p">);</span>
<span class="p">}</span>
</pre></div>
</div>
<p>The condition of the <code class="code docutils literal notranslate"><span class="pre">if</span></code> statement may be a variable declaration.
In that case, the condition is true if the value of the variable is true.
The variable will only be visible inside the <code class="code docutils literal notranslate"><span class="pre">if</span></code> block:</p>
<div class="highlight-d notranslate"><div class="highlight"><pre><span></span><span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="n">Foo</span><span class="w"> </span><span class="n">foo</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="n">getFoo</span><span class="p">())</span><span class="w"> </span><span class="p">{</span>
<span class="w"> </span><span class="c1">// do foo things here</span>
<span class="p">}</span>
</pre></div>
</div>
<p><code class="code docutils literal notranslate"><span class="pre">nullable</span> <span class="pre">Class</span></code> types are true if the class is non-null. In that case, the type
of the tested variable can be <code class="code docutils literal notranslate"><span class="pre">Class</span></code>. This is the only way in which <code class="code docutils literal notranslate"><span class="pre">nullable</span> <span class="pre">Class</span></code>
types can be converted to <code class="code docutils literal notranslate"><span class="pre">Class</span></code>.</p>
<p>The <code class="code docutils literal notranslate"><span class="pre">if</span> <span class="pre">let</span></code> form acts exactly like <code class="code docutils literal notranslate"><span class="pre">if</span></code>, except that the variable does not have to be truthy:</p>
<div class="highlight-d notranslate"><div class="highlight"><pre><span></span><span class="k">if</span><span class="w"> </span><span class="n">let</span><span class="p">(</span><span class="k">auto</span><span class="w"> </span><span class="n">bar</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="n">getFoo</span><span class="p">()?.</span><span class="n">bar</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
<span class="w"> </span><span class="c1">// bar may be false here.</span>
<span class="p">}</span>
</pre></div>
</div>
<p>The intended meaning is: “The fact that the variable was declared already indicates success.”</p>
<p>As with regular <code class="code docutils literal notranslate"><span class="pre">if</span></code>, <code class="code docutils literal notranslate"><span class="pre">breakelse</span></code> jumps to the <code class="code docutils literal notranslate"><span class="pre">else</span></code> block or past the statement.</p>
<p>This idiom is aimed at code that wants to use the result of a chain of <code class="code docutils literal notranslate"><span class="pre">?</span></code> expressions,
but doesn’t particularly care about its truth value.</p>
</section>
<section id="with-block">
<h3>With block<a class="headerlink" href="#with-block" title="Link to this heading">¶</a></h3>
<p>The <code class="code docutils literal notranslate"><span class="pre">with</span></code> block takes an expression and makes its fields implicitly accessible:</p>
<div class="highlight-d notranslate"><div class="highlight"><pre><span></span><span class="k">auto</span><span class="w"> </span><span class="n">s</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="p">(</span><span class="n">foo</span><span class="p">=</span><span class="mi">2</span><span class="p">,</span><span class="w"> </span><span class="n">bar</span><span class="p">=</span><span class="mi">3</span><span class="p">);</span>
<span class="kt">int</span><span class="w"> </span><span class="n">baz</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="mi">5</span><span class="p">;</span>
<span class="k">with</span><span class="w"> </span><span class="p">(</span><span class="n">s</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
<span class="w"> </span><span class="k">assert</span><span class="p">(</span><span class="n">foo</span><span class="w"> </span><span class="p">==</span><span class="w"> </span><span class="mi">2</span><span class="p">);</span>
<span class="w"> </span><span class="k">assert</span><span class="p">(</span><span class="n">bar</span><span class="w"> </span><span class="p">==</span><span class="w"> </span><span class="mi">3</span><span class="p">);</span>
<span class="w"> </span><span class="c1">// we can still access other variables.</span>
<span class="w"> </span><span class="k">assert</span><span class="p">(</span><span class="n">baz</span><span class="w"> </span><span class="p">==</span><span class="w"> </span><span class="mi">5</span><span class="p">);</span>
<span class="w"> </span><span class="c1">// lookup proceeds lexically, so the</span>
<span class="w"> </span><span class="c1">// variable masks the `with` statement.</span>
<span class="w"> </span><span class="kt">int</span><span class="w"> </span><span class="n">bar</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="mi">8</span><span class="p">;</span>
<span class="w"> </span><span class="k">assert</span><span class="p">(</span><span class="n">bar</span><span class="w"> </span><span class="p">==</span><span class="w"> </span><span class="mi">8</span><span class="p">);</span>
<span class="p">}</span>
</pre></div>
</div>
</section>
<section id="while-loop">
<h3>While loop<a class="headerlink" href="#while-loop" title="Link to this heading">¶</a></h3>
<p>While a condition is true, execute a statement:</p>
<div class="highlight-d notranslate"><div class="highlight"><pre><span></span><span class="n">mut</span><span class="w"> </span><span class="kt">int</span><span class="w"> </span><span class="n">i</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="mi">0</span><span class="p">;</span>
<span class="k">while</span><span class="w"> </span><span class="p">(</span><span class="n">i</span><span class="w"> </span><span class="p"><</span><span class="w"> </span><span class="mi">10</span><span class="p">)</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="n">i</span><span class="w"> </span><span class="p">+=</span><span class="w"> </span><span class="mi">1</span><span class="p">;</span><span class="w"> </span><span class="p">}</span>
</pre></div>
</div>
</section>
<section id="for-loop">
<h3>For loop<a class="headerlink" href="#for-loop" title="Link to this heading">¶</a></h3>
<p>You can loop over a range expression:</p>
<div class="highlight-d notranslate"><div class="highlight"><pre><span></span><span class="c1">// prints 2, then 3</span>
<span class="k">for</span><span class="w"> </span><span class="p">(</span><span class="nb">size_t</span><span class="w"> </span><span class="n">i</span><span class="w"> </span><span class="k">in</span><span class="w"> </span><span class="mi">2</span><span class="w"> </span><span class="p">..</span><span class="w"> </span><span class="mi">4</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
<span class="w"> </span><span class="n">print</span><span class="p">(</span><span class="n">ltoa</span><span class="p">(</span><span class="n">i</span><span class="p">));</span>
<span class="p">}</span>
</pre></div>
</div>
<p>The type of the loop variable may be left out.</p>
<p>Array expressions are ranges. Array indexes can be iterated like:</p>
<div class="highlight-d notranslate"><div class="highlight"><pre><span></span><span class="k">for</span><span class="w"> </span><span class="p">(</span><span class="n">i</span><span class="p">,</span><span class="w"> </span><span class="n">value</span><span class="w"> </span><span class="k">in</span><span class="w"> </span><span class="n">array</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
<span class="w"> </span><span class="n">array</span><span class="p">[</span><span class="n">i</span><span class="p">]</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="n">value</span><span class="w"> </span><span class="p">+</span><span class="w"> </span><span class="mi">2</span><span class="p">;</span>
<span class="p">}</span>
</pre></div>
</div>
<p>You can also use a C-style for loop:</p>
<div class="highlight-d notranslate"><div class="highlight"><pre><span></span><span class="k">for</span><span class="w"> </span><span class="p">(</span><span class="n">mut</span><span class="w"> </span><span class="kt">int</span><span class="w"> </span><span class="n">a</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="mi">0</span><span class="p">;</span><span class="w"> </span><span class="n">a</span><span class="w"> </span><span class="p"><</span><span class="w"> </span><span class="mi">10</span><span class="p">;</span><span class="w"> </span><span class="n">a</span><span class="w"> </span><span class="p">+=</span><span class="w"> </span><span class="mi">1</span><span class="p">)</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="p">}</span>
</pre></div>
</div>
<p>But this is rarely needed.</p>
</section>
<section id="break-continue">
<h3>break, continue<a class="headerlink" href="#break-continue" title="Link to this heading">¶</a></h3>
<p>While inside any loop, you may immediately abort and continue after the loop with <code class="code docutils literal notranslate"><span class="pre">break</span></code>.</p>
<p>You may immediately jump to the next iteration of the loop with <code class="code docutils literal notranslate"><span class="pre">continue</span></code>.</p>
</section>
</section>
<section id="types">
<h2>Types<a class="headerlink" href="#types" title="Link to this heading">¶</a></h2>
<section id="basic-types">
<h3>Basic types<a class="headerlink" href="#basic-types" title="Link to this heading">¶</a></h3>
<table class="docutils align-default">
<thead>
<tr class="row-odd"><th class="head"><p>name</p></th>
<th class="head"><p>meaning</p></th>
</tr>
</thead>
<tbody>
<tr class="row-even"><td><p>int</p></td>
<td><p>32-bit signed integer</p></td>
</tr>
<tr class="row-odd"><td><p>short</p></td>
<td><p>16-bit signed integer</p></td>
</tr>
<tr class="row-even"><td><p>byte</p></td>
<td><p>8-bit signed integer</p></td>
</tr>
<tr class="row-odd"><td><p>char</p></td>
<td><p>8-bit UTF-8 code unit</p></td>
</tr>
<tr class="row-even"><td><p>long</p></td>
<td><p>64-bit signed integer</p></td>
</tr>
<tr class="row-odd"><td><p>void</p></td>
<td><p>0-bit empty data</p></td>
</tr>
<tr class="row-even"><td><p>size_t</p></td>
<td><p>platform-dependent unsigned word</p></td>
</tr>
<tr class="row-odd"><td><p>float</p></td>
<td><p>32-bit IEEE floating point number</p></td>
</tr>
<tr class="row-even"><td><p>double</p></td>
<td><p>64-bit IEEE floating point number</p></td>
</tr>
</tbody>
</table>
</section>
<section id="array">
<h3>Array<a class="headerlink" href="#array" title="Link to this heading">¶</a></h3>
<p>The type <code class="code docutils literal notranslate"><span class="pre">T[]</span></code> is an “array of T”, which some languages call a slice.
It consists of a pointer, a length and a reference to the array object.</p>
<p><code class="code docutils literal notranslate"><span class="pre">[2]</span></code> is an array of ints (<code class="code docutils literal notranslate"><span class="pre">int[]</span></code>), allocated on the heap.</p>
<p><code class="code docutils literal notranslate"><span class="pre">array</span> <span class="pre">~</span> <span class="pre">array</span></code> is the concatenation of two arrays.</p>
<p>Concatenation is the only way to add elements to the array. The values in an array
cannot be directly modified! In other words, arrays are immutable by default.</p>
<p><code class="code docutils literal notranslate"><span class="pre">array.length</span></code> is the length of the array.</p>
<p>Appending to an array in a loop will follow a doubling strategy. It should be reasonably efficient.</p>
<p><code class="code docutils literal notranslate"><span class="pre">array[2]</span></code> is the third element (base-0) of the array.</p>
<p><code class="code docutils literal notranslate"><span class="pre">array.dup</span></code> creates a copy of the array. The copy will be mutable.</p>
</section>
<section id="mutable-array">
<h3>Mutable Array<a class="headerlink" href="#mutable-array" title="Link to this heading">¶</a></h3>
<p>The type <code class="code docutils literal notranslate"><span class="pre">T</span> <span class="pre">mut[]</span></code> is a “mutable array of T”. It differs from normal arrays in that elements
can be freely reassigned.</p>
<p><code class="code docutils literal notranslate"><span class="pre">array.freeze</span></code> converts <code class="code docutils literal notranslate"><span class="pre">array</span></code> to an immutable array. Unless <code class="code docutils literal notranslate"><span class="pre">array</span></code> has exactly one
reference, this operation is forbidden; using the <code class="code docutils literal notranslate"><span class="pre">array</span></code> variable after this expression
has been evaluated is forbidden. (The compiler does not at present enforce this, but it
will in the future.)</p>
<p><code class="code docutils literal notranslate"><span class="pre">T[]</span></code> and <code class="code docutils literal notranslate"><span class="pre">T</span> <span class="pre">mut[]</span></code> are separated because in my experience these types occupy fundamentally
different roles in a program. If you pass an array to a function, you get the
assurance that it won’t be modified. Likewise if you are a class, and somebody gives you an
array that you store in a class member, the value of that array will not change on you.</p>
</section>
<section id="tuple">
<h3>Tuple<a class="headerlink" href="#tuple" title="Link to this heading">¶</a></h3>
<p><code class="code docutils literal notranslate"><span class="pre">(int,</span> <span class="pre">float)</span></code> is a tuple with two member types, <code class="code docutils literal notranslate"><span class="pre">int</span></code> and <code class="code docutils literal notranslate"><span class="pre">float</span></code>. Each member can have an independent value.</p>
<p><code class="code docutils literal notranslate"><span class="pre">(2,</span> <span class="pre">3.0f)</span></code> is an expression of type <code class="code docutils literal notranslate"><span class="pre">(int,</span> <span class="pre">float)</span></code>.</p>
<p><code class="code docutils literal notranslate"><span class="pre">tuple[0]</span></code> is the first member of the tuple. The index value must be an int literal.</p>
<p>Tuple members can be named: <code class="code docutils literal notranslate"><span class="pre">(int</span> <span class="pre">i,</span> <span class="pre">float</span> <span class="pre">f)</span></code>. This allows accessing the member with <code class="code docutils literal notranslate"><span class="pre">value.i</span></code>.</p>
<p>When implicitly converting tuples, tuple fields without names implicitly convert to any name, but tuple
fields with names only convert to other fields with the same name.</p>
<p>For example, <code class="code docutils literal notranslate"><span class="pre">(2,</span> <span class="pre">3)</span></code> implicitly converts to <code class="code docutils literal notranslate"><span class="pre">(int</span> <span class="pre">from,</span> <span class="pre">int</span> <span class="pre">to)</span></code>, but <code class="code docutils literal notranslate"><span class="pre">(min=2,</span> <span class="pre">max=3)</span></code> does not.</p>
</section>
<section id="pointers">
<h3>Pointers<a class="headerlink" href="#pointers" title="Link to this heading">¶</a></h3>
<p>Don’t use pointers.</p>
</section>
<section id="sum-type">
<h3>Sum type<a class="headerlink" href="#sum-type" title="Link to this heading">¶</a></h3>
<p><code class="code docutils literal notranslate"><span class="pre">(int</span> <span class="pre">|</span> <span class="pre">float)</span></code> is either an int or a float value:</p>
<div class="highlight-d notranslate"><div class="highlight"><pre><span></span><span class="p">(</span><span class="kt">int</span><span class="w"> </span><span class="p">|</span><span class="w"> </span><span class="kt">float</span><span class="p">)</span><span class="w"> </span><span class="n">a</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="mi">4</span><span class="p">;</span>
<span class="k">return</span><span class="w"> </span><span class="n">a</span><span class="p">.</span><span class="k">case</span><span class="p">(</span>
<span class="w"> </span><span class="kt">int</span><span class="w"> </span><span class="n">i</span><span class="p">:</span><span class="w"> </span><span class="n">i</span><span class="w"> </span><span class="p">/</span><span class="w"> </span><span class="mi">2</span><span class="p">,</span>
<span class="w"> </span><span class="kt">float</span><span class="w"> </span><span class="n">f</span><span class="p">:</span><span class="w"> </span><span class="n">f</span><span class="w"> </span><span class="p">/</span><span class="w"> </span><span class="mf">2.0f</span><span class="p">);</span>
<span class="n">a</span><span class="p">.</span><span class="k">case</span><span class="w"> </span><span class="p">{</span>
<span class="w"> </span><span class="kt">int</span><span class="w"> </span><span class="n">i</span><span class="p">:</span>
<span class="w"> </span><span class="n">print</span><span class="p">(</span><span class="n">itoa</span><span class="p">(</span><span class="n">i</span><span class="p">));</span>
<span class="w"> </span><span class="kt">float</span><span class="w"> </span><span class="n">f</span><span class="p">:</span>
<span class="w"> </span><span class="n">print</span><span class="p">(</span><span class="n">ftoa</span><span class="p">(</span><span class="n">f</span><span class="p">));</span>
<span class="p">}</span>
</pre></div>
</div>
<p>Members of a sumtype can be marked as “fail”, enabling error return:</p>
<div class="highlight-d notranslate"><div class="highlight"><pre><span></span><span class="p">(</span><span class="kt">int</span><span class="w"> </span><span class="p">|</span><span class="w"> </span><span class="n">fail</span><span class="w"> </span><span class="n">FileNotFound</span><span class="p">)</span><span class="w"> </span><span class="n">foo</span><span class="p">()</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="k">return</span><span class="w"> </span><span class="s">"test"</span><span class="p">.</span><span class="n">readAll</span><span class="p">?.</span><span class="n">itoa</span><span class="p">;</span><span class="w"> </span><span class="p">}</span>
<span class="kt">int</span><span class="w"> </span><span class="n">i</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="n">foo</span><span class="p">()?;</span>
</pre></div>
</div>
<p>If foo returns a <code class="code docutils literal notranslate"><span class="pre">FileNotFound</span></code>, it will be automatically returned at the <code class="code docutils literal notranslate"><span class="pre">?</span></code>.</p>
<p>Note that this is not required for subtypes of <code class="code docutils literal notranslate"><span class="pre">std.error.Error</span></code>.</p>
</section>
<section id="symbol-identifier">
<h3>Symbol Identifier<a class="headerlink" href="#symbol-identifier" title="Link to this heading">¶</a></h3>
<p>A symbol identifier takes the form <code class="code docutils literal notranslate"><span class="pre">:name</span></code>.</p>
<p>It is both a type and an expression. The type <code class="code docutils literal notranslate"><span class="pre">:name</span></code> has one value, which is also <code class="code docutils literal notranslate"><span class="pre">:name</span></code>.</p>
<p>This feature can be used to “type-tag” entries in sumtypes, to differentiate identically
typed entries, such as <code class="code docutils literal notranslate"><span class="pre">(:centimeters,</span> <span class="pre">int</span> <span class="pre">|</span> <span class="pre">:meters,</span> <span class="pre">int)</span></code>.</p>
<p>It is also used to construct “value-less” sumtype entries, such as <code class="code docutils literal notranslate"><span class="pre">(int</span> <span class="pre">|</span> <span class="pre">:none)</span></code>.</p>
</section>
<section id="struct">
<h3>Struct<a class="headerlink" href="#struct" title="Link to this heading">¶</a></h3>
<p>A struct is a value type that combines various members and methods that operate on them:</p>
<div class="highlight-d notranslate"><div class="highlight"><pre><span></span><span class="k">struct</span><span class="w"> </span><span class="n">Foo</span>
<span class="p">{</span>
<span class="w"> </span><span class="kt">int</span><span class="w"> </span><span class="n">a</span><span class="p">,</span><span class="w"> </span><span class="n">b</span><span class="p">;</span>
<span class="w"> </span><span class="kt">int</span><span class="w"> </span><span class="n">sum</span><span class="p">()</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="k">return</span><span class="w"> </span><span class="k">this</span><span class="p">.</span><span class="n">a</span><span class="w"> </span><span class="p">+</span><span class="w"> </span><span class="n">b</span><span class="p">;</span><span class="w"> </span><span class="p">}</span>
<span class="p">}</span>
<span class="n">Foo</span><span class="w"> </span><span class="n">foo</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="n">Foo</span><span class="p">(</span><span class="mi">2</span><span class="p">,</span><span class="w"> </span><span class="mi">3</span><span class="p">);</span>
<span class="k">assert</span><span class="p">(</span><span class="n">foo</span><span class="p">.</span><span class="n">sum</span><span class="p">()</span><span class="w"> </span><span class="p">==</span><span class="w"> </span><span class="mi">5</span><span class="p">);</span>
</pre></div>
</div>
<p>A method is a function defined in a struct (or class). It takes a reference to the struct value it is called
on as a hidden parameter called <code class="code docutils literal notranslate"><span class="pre">this</span></code>.</p>
</section>
<section id="class">
<h3>Class<a class="headerlink" href="#class" title="Link to this heading">¶</a></h3>
<p>A class is a <strong>reference type</strong> that combines various members and methods that operate on them:</p>
<div class="highlight-d notranslate"><div class="highlight"><pre><span></span><span class="k">class</span><span class="w"> </span><span class="n">Foo</span>
<span class="p">{</span>
<span class="w"> </span><span class="kt">int</span><span class="w"> </span><span class="n">a</span><span class="p">,</span><span class="w"> </span><span class="n">b</span><span class="p">;</span>
<span class="w"> </span><span class="k">this</span><span class="p">(</span><span class="k">this</span><span class="p">.</span><span class="n">a</span><span class="p">,</span><span class="w"> </span><span class="k">this</span><span class="p">.</span><span class="n">b</span><span class="p">)</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="p">}</span>
<span class="w"> </span><span class="kt">int</span><span class="w"> </span><span class="n">sum</span><span class="p">()</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="k">return</span><span class="w"> </span><span class="k">this</span><span class="p">.</span><span class="n">a</span><span class="w"> </span><span class="p">+</span><span class="w"> </span><span class="n">b</span><span class="p">;</span><span class="w"> </span><span class="p">}</span>
<span class="p">}</span>
<span class="n">Foo</span><span class="w"> </span><span class="n">foo</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="k">new</span><span class="w"> </span><span class="n">Foo</span><span class="p">(</span><span class="mi">2</span><span class="p">,</span><span class="w"> </span><span class="mi">3</span><span class="p">);</span>
<span class="k">assert</span><span class="p">(</span><span class="n">foo</span><span class="p">.</span><span class="n">sum</span><span class="p">()</span><span class="w"> </span><span class="p">==</span><span class="w"> </span><span class="mi">5</span><span class="p">);</span>
</pre></div>
</div>
<p>Note that, as opposed to C++, the type <code class="code docutils literal notranslate"><span class="pre">Foo</span></code> designates a reference to the class. It is impossible
to hold a class by value.</p>
<p><code class="code docutils literal notranslate"><span class="pre">this</span></code> is a special method without return value that designates the constructor of the class. When instantiating
a class with <code class="code docutils literal notranslate"><span class="pre">new</span> <span class="pre">Class(args)</span></code>, <code class="code docutils literal notranslate"><span class="pre">this(args)</span></code> is called.</p>
<p>The parameter <code class="code docutils literal notranslate"><span class="pre">this.a</span></code> indicates that the argument is directly assigned to the member <code class="code docutils literal notranslate"><span class="pre">a</span></code>,
rather than passed to the method as a parameter.</p>
<p>Classes can be inherited with a subclass. An instance of the subclass can be implicitly converted to
the parent class. When a method is called on an instance, the function that runs is that of the
allocated class, not of the type of the reference:</p>
<div class="highlight-d notranslate"><div class="highlight"><pre><span></span><span class="k">class</span><span class="w"> </span><span class="n">Foo</span>
<span class="p">{</span>
<span class="w"> </span><span class="kt">int</span><span class="w"> </span><span class="n">get</span><span class="p">()</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="k">return</span><span class="w"> </span><span class="mi">5</span><span class="p">;</span><span class="w"> </span><span class="p">}</span>
<span class="p">}</span>
<span class="k">class</span><span class="w"> </span><span class="n">Bar</span><span class="w"> </span><span class="p">:</span><span class="w"> </span><span class="n">Foo</span>
<span class="p">{</span>
<span class="w"> </span><span class="c1">// "override" must be specified, to indicate</span>
<span class="w"> </span><span class="c1">// that a parent method is being redefined.</span>
<span class="w"> </span><span class="k">override</span><span class="w"> </span><span class="kt">int</span><span class="w"> </span><span class="n">get</span><span class="p">()</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="k">return</span><span class="w"> </span><span class="mi">7</span><span class="p">;</span><span class="w"> </span><span class="p">}</span>
<span class="p">}</span>
<span class="n">Foo</span><span class="w"> </span><span class="n">foo</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="k">new</span><span class="w"> </span><span class="n">Bar</span><span class="p">;</span>
<span class="k">assert</span><span class="p">(</span><span class="n">foo</span><span class="p">.</span><span class="n">get</span><span class="w"> </span><span class="p">==</span><span class="w"> </span><span class="mi">7</span><span class="p">);</span>
</pre></div>
</div>
<p>Classes can also inherit from interfaces, which are like “thin classes” that can only contain methods.
In exchange, arbitrarily many interfaces can be inherited from:</p>
<div class="highlight-d notranslate"><div class="highlight"><pre><span></span><span class="k">interface</span><span class="w"> </span><span class="n">Foo</span>
<span class="p">{</span>
<span class="w"> </span><span class="kt">int</span><span class="w"> </span><span class="n">get</span><span class="p">();</span>
<span class="p">}</span>
<span class="k">class</span><span class="w"> </span><span class="n">Bar</span><span class="w"> </span><span class="p">:</span><span class="w"> </span><span class="n">Parent</span><span class="p">,</span><span class="w"> </span><span class="n">Foo</span>
<span class="p">{</span>
<span class="w"> </span><span class="k">override</span><span class="w"> </span><span class="kt">int</span><span class="w"> </span><span class="n">get</span><span class="p">()</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="k">return</span><span class="w"> </span><span class="mi">5</span><span class="p">;</span><span class="w"> </span><span class="p">}</span>
<span class="p">}</span>
<span class="n">Foo</span><span class="w"> </span><span class="n">foo</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="k">new</span><span class="w"> </span><span class="n">Bar</span><span class="p">;</span>
<span class="k">assert</span><span class="p">(</span><span class="n">foo</span><span class="p">.</span><span class="n">get</span><span class="w"> </span><span class="p">==</span><span class="w"> </span><span class="mi">5</span><span class="p">);</span>
</pre></div>
</div>
<p>In a subclass constructor, you can use the syntax <code class="code docutils literal notranslate"><span class="pre">super()</span></code> to call the constructor of the parent class.</p>
<p>You can also use the keyword <code class="code docutils literal notranslate"><span class="pre">super</span></code> in the parameter list to insert an implicit super constructor call:</p>
<div class="highlight-d notranslate"><div class="highlight"><pre><span></span><span class="k">class</span><span class="w"> </span><span class="n">Bar</span><span class="w"> </span><span class="p">:</span><span class="w"> </span><span class="n">Foo</span>
<span class="p">{</span>
<span class="w"> </span><span class="kt">int</span><span class="w"> </span><span class="n">c</span><span class="p">;</span>
<span class="w"> </span><span class="k">this</span><span class="p">(</span><span class="k">super</span><span class="p">,</span><span class="w"> </span><span class="k">this</span><span class="p">.</span><span class="n">c</span><span class="p">)</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="p">}</span>
<span class="p">}</span>
</pre></div>
</div>
<p>The type of an object can be tested with the <code class="code docutils literal notranslate"><span class="pre">instanceOf</span></code> property:</p>
<div class="highlight-d notranslate"><div class="highlight"><pre><span></span><span class="n">nullable</span><span class="w"> </span><span class="n">Bar</span><span class="w"> </span><span class="n">bar</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="n">foo</span><span class="p">.</span><span class="n">instanceOf</span><span class="p">(</span><span class="n">Bar</span><span class="p">);</span>
<span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="n">Bar</span><span class="w"> </span><span class="n">bar</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="n">foo</span><span class="p">.</span><span class="n">instanceOf</span><span class="p">(</span><span class="n">Bar</span><span class="p">))</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="p">}</span>
</pre></div>
</div>
<p>Return and parameter types follow <a class="reference external" href="https://en.wikipedia.org/wiki/Covariance_and_contravariance_(computer_science)">covariance and contravariance</a> on inheritance.</p>
<p>A class type may be qualified as <code class="code docutils literal notranslate"><span class="pre">nullable</span></code>. In that case, the special value
<code class="code docutils literal notranslate"><span class="pre">null</span></code> implicitly converts to a reference to the type. By default, class references are not
nullable:</p>
<div class="highlight-d notranslate"><div class="highlight"><pre><span></span><span class="n">nullable</span><span class="w"> </span><span class="n">Foo</span><span class="w"> </span><span class="n">foo</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="kc">null</span><span class="p">;</span>
<span class="k">assert</span><span class="p">(!</span><span class="n">foo</span><span class="p">);</span>
<span class="n">Foo</span><span class="w"> </span><span class="n">bar</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="n">foo</span><span class="p">;</span><span class="w"> </span><span class="c1">// errors</span>
</pre></div>
</div>
<p>As a special treat, the <code class="code docutils literal notranslate"><span class="pre">case</span></code> expression allows treating a nullable class as a sumtype
of a non-nullable class and <code class="code docutils literal notranslate"><span class="pre">null</span></code>:</p>
<div class="highlight-d notranslate"><div class="highlight"><pre><span></span><span class="n">nullable</span><span class="w"> </span><span class="n">Foo</span><span class="w"> </span><span class="n">foo</span><span class="p">;</span>
<span class="n">Foo</span><span class="w"> </span><span class="n">bar</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="n">foo</span><span class="p">.</span><span class="k">case</span><span class="p">(</span><span class="kc">null</span><span class="p">:</span><span class="w"> </span><span class="k">return</span><span class="w"> </span><span class="kc">false</span><span class="p">);</span>
</pre></div>
</div>
</section>
<section id="function-and-delegate">
<h3>Function and Delegate<a class="headerlink" href="#function-and-delegate" title="Link to this heading">¶</a></h3>
<p>You can take the address of a function using the <code class="code docutils literal notranslate"><span class="pre">&</span></code> operator. The type of
the expression is <code class="code docutils literal notranslate"><span class="pre">R</span> <span class="pre">function(T)</span></code>.</p>
<p>When you take the address of a class method, the type will be <code class="code docutils literal notranslate"><span class="pre">R</span> <span class="pre">delegate(T)</span></code>.
A <code class="code docutils literal notranslate"><span class="pre">delegate</span></code> is a “fat function pointer” that carries a pointer to the context,
ie. the object.</p>
<p>You can also take the address of a nested function with <code class="code docutils literal notranslate"><span class="pre">&</span></code>, but then the type
will be <code class="code docutils literal notranslate"><span class="pre">R</span> <span class="pre">delegate!(T)</span></code>, a “noncopyable delegate”. It cannot be used anywhere
where a reference would have to be taken. As the delegate carries a pointer to
the stackframe, this is necessary to protect the developer from use-after-return
bugs.</p>
<p>A nested function can be heap-allocated using the syntax <code class="code docutils literal notranslate"><span class="pre">new</span> <span class="pre">&fun</span></code>. <code class="code docutils literal notranslate"><span class="pre">new</span></code> will
make a copy of the surrounding stackframe for the function. In that case,
the type will be <code class="code docutils literal notranslate"><span class="pre">R</span> <span class="pre">delegate(T)</span></code> and the allocated stackframe will be reference
counted.</p>
</section>
<section id="typeof">
<h3><code class="code docutils literal notranslate"><span class="pre">typeof</span></code><a class="headerlink" href="#typeof" title="Link to this heading">¶</a></h3>
<p>Given an expression, the type of the expression can be used as a type with <code class="code docutils literal notranslate"><span class="pre">typeof</span></code>:</p>
<div class="highlight-d notranslate"><div class="highlight"><pre><span></span><span class="k">typeof</span><span class="p">(</span><span class="n">a</span><span class="w"> </span><span class="p">+</span><span class="w"> </span><span class="n">b</span><span class="p">)</span><span class="w"> </span><span class="n">sum</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="n">a</span><span class="w"> </span><span class="p">+</span><span class="w"> </span><span class="n">b</span>
</pre></div>
</div>
<p>Since <code class="code docutils literal notranslate"><span class="pre">auto</span></code> exists, this is mostly used for return and parameter types.</p>
</section>
</section>
<section id="unittest">
<h2>Unittest<a class="headerlink" href="#unittest" title="Link to this heading">¶</a></h2>
<p>Unittest blocks will be compiled and run when the compiler is called with <code class="code docutils literal notranslate"><span class="pre">-unittest</span></code>:</p>
<div class="highlight-d notranslate"><div class="highlight"><pre><span></span><span class="kt">int</span><span class="w"> </span><span class="n">sum</span><span class="p">(</span><span class="kt">int</span><span class="w"> </span><span class="n">a</span><span class="p">,</span><span class="w"> </span><span class="kt">int</span><span class="w"> </span><span class="n">b</span><span class="p">)</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="k">return</span><span class="w"> </span><span class="n">a</span><span class="w"> </span><span class="p">+</span><span class="w"> </span><span class="n">b</span><span class="p">;</span><span class="w"> </span><span class="p">}</span>
<span class="k">unittest</span>
<span class="p">{</span>
<span class="w"> </span><span class="k">assert</span><span class="p">(</span><span class="n">sum</span><span class="p">(</span><span class="mi">2</span><span class="p">,</span><span class="w"> </span><span class="mi">3</span><span class="p">)</span><span class="w"> </span><span class="p">==</span><span class="w"> </span><span class="mi">5</span><span class="p">);</span>
<span class="p">}</span>
</pre></div>
</div>
</section>
<section id="templates">
<h2>Templates<a class="headerlink" href="#templates" title="Link to this heading">¶</a></h2>
<p>A template is a wrapper around a declaration that allows parameterizing it.
The syntax is:</p>
<div class="highlight-d notranslate"><div class="highlight"><pre><span></span><span class="k">template</span><span class="w"> </span><span class="n">max</span><span class="p">(</span><span class="n">T</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
<span class="w"> </span><span class="n">T</span><span class="w"> </span><span class="n">max</span><span class="p">(</span><span class="n">T</span><span class="w"> </span><span class="n">first</span><span class="p">,</span><span class="w"> </span><span class="n">T</span><span class="w"> </span><span class="n">second</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
<span class="w"> </span><span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="n">first</span><span class="w"> </span><span class="p">></span><span class="w"> </span><span class="n">second</span><span class="p">)</span><span class="w"> </span><span class="k">return</span><span class="w"> </span><span class="n">first</span><span class="p">;</span>
<span class="w"> </span><span class="k">return</span><span class="w"> </span><span class="n">second</span><span class="p">;</span>
<span class="w"> </span><span class="p">}</span>
<span class="p">}</span>
</pre></div>
</div>
<p>Here, <code class="code docutils literal notranslate"><span class="pre">T</span></code> is the “template parameter”. Multiple template parameters can be used.</p>
<p>The symbol in the template must be <em>eponymous</em>, ie. have the same name as the template. To call it,
instantiate the template: <code class="code docutils literal notranslate"><span class="pre">max!int(2,</span> <span class="pre">3)</span></code> or <code class="code docutils literal notranslate"><span class="pre">max!float(2.5,</span> <span class="pre">3)</span></code>. Here, <code class="code docutils literal notranslate"><span class="pre">max!int</span></code> is “the function <code class="code docutils literal notranslate"><span class="pre">max</span></code>
in the version of the template <code class="code docutils literal notranslate"><span class="pre">max</span></code> where <code class="code docutils literal notranslate"><span class="pre">T</span></code> is <code class="code docutils literal notranslate"><span class="pre">int</span></code>.”</p>
<p>Multiple parameters are passed in parentheses: <code class="code docutils literal notranslate"><span class="pre">templ!(int,</span> <span class="pre">float)</span></code>.</p>
<p>If the template is called directly, without explicitly instantiating it, the compiler will
try to unify the arguments passed with the template arguments available in order to infer their types.
If only some template arguments are given in the instantiation, the compiler will try to infer the rest.</p>
</section>
<section id="ranges">
<h2>Ranges<a class="headerlink" href="#ranges" title="Link to this heading">¶</a></h2>
<p>If a type <code class="code docutils literal notranslate"><span class="pre">T</span></code> has the properties <code class="code docutils literal notranslate"><span class="pre">bool</span> <span class="pre">empty</span></code>, <code class="code docutils literal notranslate"><span class="pre">T</span> <span class="pre">next</span></code> and <code class="code docutils literal notranslate"><span class="pre">E</span> <span class="pre">front</span></code>, then it is called a “range over <code class="code docutils literal notranslate"><span class="pre">E</span></code>”.</p>
<p>Arrays are an example of such.</p>
<p>Another example is range expressions: <code class="code docutils literal notranslate"><span class="pre">from</span> <span class="pre">..</span> <span class="pre">to</span></code>.</p>
<p>If you define these properties in a data type, you can use it as the source of a loop.</p>
</section>
<section id="lambdas">
<h2>Lambdas<a class="headerlink" href="#lambdas" title="Link to this heading">¶</a></h2>
<p>A lambda is a templated nested function reference. They can be assigned to a value. When called, they
are implicitly instantiated.</p>
<p>Example:</p>
<div class="highlight-d notranslate"><div class="highlight"><pre><span></span><span class="kt">int</span><span class="w"> </span><span class="n">a</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="mi">5</span><span class="p">;</span>
<span class="k">auto</span><span class="w"> </span><span class="n">add</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="n">b</span><span class="w"> </span><span class="p">=></span><span class="w"> </span><span class="n">a</span><span class="w"> </span><span class="p">+</span><span class="w"> </span><span class="n">b</span><span class="p">;</span>
<span class="k">assert</span><span class="p">(</span><span class="n">add</span><span class="p">(</span><span class="mi">2</span><span class="p">)</span><span class="w"> </span><span class="p">==</span><span class="w"> </span><span class="mi">7</span><span class="p">);</span>
</pre></div>
</div>
<p>Every lambda has a unique type. Because of this, they cannot be stored in data structures.
Their primary purpose is being passed to templated functions:</p>
<div class="highlight-d notranslate"><div class="highlight"><pre><span></span><span class="k">auto</span><span class="w"> </span><span class="n">a</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="p">(</span><span class="mi">0</span><span class="w"> </span><span class="p">..</span><span class="w"> </span><span class="mi">10</span><span class="p">).</span><span class="n">filter</span><span class="p">(</span><span class="n">a</span><span class="w"> </span><span class="p">=></span><span class="w"> </span><span class="n">a</span><span class="w"> </span><span class="p">&</span><span class="w"> </span><span class="mi">1</span><span class="w"> </span><span class="p">==</span><span class="w"> </span><span class="mi">0</span><span class="p">).</span><span class="n">map</span><span class="p">(</span><span class="n">a</span><span class="w"> </span><span class="p">=></span><span class="w"> </span><span class="n">a</span><span class="w"> </span><span class="p">/</span><span class="w"> </span><span class="mi">2</span><span class="p">).</span><span class="n">array</span><span class="p">;</span>
<span class="k">assert</span><span class="p">(</span><span class="n">a</span><span class="w"> </span><span class="p">==</span><span class="w"> </span><span class="p">[</span><span class="mi">0</span><span class="p">,</span><span class="w"> </span><span class="mi">1</span><span class="p">,</span><span class="w"> </span><span class="mi">2</span><span class="p">,</span><span class="w"> </span><span class="mi">3</span><span class="p">,</span><span class="w"> </span><span class="mi">4</span><span class="p">]);</span>
</pre></div>
</div>
<p>The compiler will try to prevent you from returning a lambda from the function where it was defined.
To enable this, lambdas cannot be assigned to class fields, or in general put in any location where
the compiler could lose track of where the lambda is.</p>
</section>
<section id="macros">
<h2>Macros<a class="headerlink" href="#macros" title="Link to this heading">¶</a></h2>
<div class="admonition note">
<p class="admonition-title">Note</p>
<p>For this feature, compiler knowledge is required!</p>
</div>
<p>When <code class="code docutils literal notranslate"><span class="pre">macro(function)</span></code> is called, <code class="code docutils literal notranslate"><span class="pre">function</span></code> is loaded into the compiler and executed with a macro state
parameter. This allows modifying the macro state of the compiler to add a macro class instance.
Macro classes can extend the compiler with new functionality using a set of hooks:</p>
<ul class="simple">
<li><p>calls: <code class="code docutils literal notranslate"><span class="pre">a(b,</span> <span class="pre">c)</span></code></p></li>
<li><p>expressions: <code class="code docutils literal notranslate"><span class="pre">2</span> <span class="pre">★</span> <span class="pre">2</span></code></p></li>
<li><p>properties: <code class="code docutils literal notranslate"><span class="pre">a.b<property</span> <span class="pre">goes</span> <span class="pre">here></span></code></p></li>
<li><p>statements: <code class="code docutils literal notranslate"><span class="pre">macroThing;</span></code></p></li>
<li><p>imports: <code class="code docutils literal notranslate"><span class="pre">import</span> <span class="pre">github("http://github.com/neat-lang/example").module;</span></code></p></li>
</ul>
<p>Look at <code class="code docutils literal notranslate"><span class="pre">std.macro.*</span></code> for examples.</p>
<p>The entire compiler is available for importing and reuse in macros. However, it is recommended
to limit yourself to the functionality in <code class="code docutils literal notranslate"><span class="pre">neat.base</span></code>. This will also keep compile times down.</p>
</section>
</section>
</div>
</div>
</div>
<div class="sphinxsidebar" role="navigation" aria-label="main navigation">
<div class="sphinxsidebarwrapper">
<h1 class="logo"><a href="index.html">Neat</a></h1>
<h3>Navigation</h3>
<ul class="current">
<li class="toctree-l1"><a class="reference internal" href="getstarted.html">Getting Started</a></li>
<li class="toctree-l1"><a class="reference internal" href="intro.html">Introduction</a></li>
<li class="toctree-l1 current"><a class="current reference internal" href="#">Manual</a><ul>
<li class="toctree-l2"><a class="reference internal" href="#lexical">Lexical</a></li>
<li class="toctree-l2"><a class="reference internal" href="#modules">Modules</a></li>
<li class="toctree-l2"><a class="reference internal" href="#packages">Packages</a></li>
<li class="toctree-l2"><a class="reference internal" href="#module-level-statements">Module-Level Statements</a></li>
<li class="toctree-l2"><a class="reference internal" href="#expressions">Expressions</a></li>
<li class="toctree-l2"><a class="reference internal" href="#functions">Functions</a></li>
<li class="toctree-l2"><a class="reference internal" href="#statements">Statements</a></li>
<li class="toctree-l2"><a class="reference internal" href="#types">Types</a></li>
<li class="toctree-l2"><a class="reference internal" href="#unittest">Unittest</a></li>
<li class="toctree-l2"><a class="reference internal" href="#templates">Templates</a></li>
<li class="toctree-l2"><a class="reference internal" href="#ranges">Ranges</a></li>
<li class="toctree-l2"><a class="reference internal" href="#lambdas">Lambdas</a></li>
<li class="toctree-l2"><a class="reference internal" href="#macros">Macros</a></li>
</ul>
</li>
<li class="toctree-l1"><a class="reference internal" href="std.html">Standard Library</a></li>
<li class="toctree-l1"><a class="reference external" href="https://github.com/neat-lang/neat/">Neat on Github 🔗</a></li>
</ul>
<div id="searchbox" style="display: none" role="search">
<h3 id="searchlabel">Quick search</h3>
<div class="searchformwrapper">
<form class="search" action="search.html" method="get">
<input type="text" name="q" aria-labelledby="searchlabel" autocomplete="off" autocorrect="off" autocapitalize="off" spellcheck="false"/>
<input type="submit" value="Go" />
</form>
</div>
</div>
<script>document.getElementById('searchbox').style.display = "block"</script>
</div>
</div>
<div class="clearer"></div>
</div>
<div class="footer">
©@FeepingCreature.
|
Powered by <a href="http://sphinx-doc.org/">Sphinx 7.2.6</a>
& <a href="https://github.com/bitprophet/alabaster">Alabaster 0.7.13</a>
|
<a href="_sources/manual.rst.txt"
rel="nofollow">Page source</a>
</div>
</body>
</html>