forked from WheretIB/nullc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
LanguageEN.html
2751 lines (2636 loc) · 190 KB
/
LanguageEN.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
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<!DOCTYPE html>
<html>
<head>
<title>NULLC Language reference</title>
<style type="text/css">
p.code
{
background: #eee;
font-family: Consolas, Courier New, monospace;
font-size: 10pt;
}
span.code
{
background: #eee;
font-family: Consolas, Courier New, monospace;
font-size: 10pt;
}
pre.code{ background: #eee; font-family: Consolas, Courier New, monospace; font-size: 10pt; }
span.rword
{
color: #00f;
}
span.func
{
color: #880000;
font-style: italic;
}
span.var
{
color: #555;
}
span.real
{
color: #008800;
}
span.comment
{
color: #f0f;
}
span.string
{
color: #880000;
}
span.vardef{color: #323232;}
span.bold{font-weight: bold;}
span.error{color: #f00;text-decoration: underline;}
div.topic
{
margin-left: 20px;
}
div.subtopic
{
margin-left: 20px;
}
p.example_head
{
font-weight: bold;
cursor: pointer;
background-color: #dfd;
}
p.example_body
{
display: none;
margin-left: 10px;
}
pre.example_body
{
display: none;
margin-left: 10px;
}
div.example
{
}
div.function
{
border: 1px dashed #000;
margin-bottom: 0.5em;
padding-left: 10px;
}
div.function > p
{
margin-top: 0px;
margin-left: 40px;
}
</style>
<script language="javascript" type="text/javascript">
var hashMap = new Array();
function toggleView(a, b, c)
{
if(hashMap[a] == undefined)
hashMap[a] = 1;
else
hashMap[a] = 1 - hashMap[a];
document.getElementById(b).style.display = hashMap[a] ? "block" : "none";
document.getElementById(a).innerHTML = (hashMap[a] ? "Hide " : "Show ") + c;
}
</script>
</head>
<body>
<h2 align="center">NULLC Language reference</h2>
<ol>
<li>General</li>
<ol>
<li><a href="#basictypes">Basic types</a></li>
<li><a href="#varalign">Variable alignment</a></li>
<li><a href="#typeauto">Type inference</a></li>
</ol>
<li>Expressions</li>
<ol>
<li><a href="#import">import expression</a></li>
<li><a href="#typeof">typeof expression</a></li>
<ol>
<li><a href="#typeof_ext">Extended typeof expressions</a></li>
</ol>
<li><a href="#sizeof">sizeof expression</a></li>
<li><a href="#new">new expression</a></li>
<ol>
<li><a href="#gc">Garbage collection</a></li>
<li><a href="#constructor">Constructor call</a></li>
<li><a href="#finalize">Object finalization</a></li>
</ol>
<li><a href="#break">break expression</a></li>
<li><a href="#continue">continue expression</a></li>
<li><a href="#return">return expression</a></li>
<li><a href="#yield">yield expression</a></li>
<li><a href="#typedef">typedef expression</a></li>
</ol>
<li>Statements</li>
<ol>
<li><a href="#if">if statement</a></li>
<ol>
<li><a href="#static_if">Static if</a></li>
</ol>
<li><a href="#for">for statement</a></li>
<ol>
<li><a href="#forsimple">C-style for</a></li>
<li><a href="#foreach">for each</a></li>
<li><a href="#foreachiternator">Custom iterator</a></li>
</ol>
<li><a href="#while">while statement</a></li>
<li><a href="#do">do...while statement</a></li>
<li><a href="#switch">switch statement</a></li>
</ol>
<li>Declarations</li>
<ol>
<li><a href="#classes">User classes</a></li>
<ol>
<li><a href="#classaccessor">Accessors</a></li>
<li><a href="#classgeneric">Generic classes</a></li>
<ol>
<li><a href="#classgenericspec">Generic class member type specialization</a></li>
</ol>
<li><a href="#classconstructor">Class constructor</a></li>
<li><a href="#classforward">Class forward declaration</a></li>
</ol>
<li><a href="#vardef">Variables</a></li>
<li><a href="#functions">Functions</a></li>
<ol>
<li><a href="#varargs">Variable argument list</a></li>
<li><a href="#funclocal">Local functions</a></li>
<li><a href="#funcclosure">Closures</a></li>
<li><a href="#funcmember">Member functions</a></li>
<li><a href="#funcinline">Function Literals</a></li>
<li><a href="#funcshort">Short inline functions</a></li>
<li><a href="#funcoverload">Function overloading</a></li>
<li><a href="#funcoperators">Operator overloading</a></li>
<li><a href="#coroutine">Coroutines</a></li>
<li><a href="#generic">Generic functions</a></li>
<li><a href="#genericspec">Generic function specialization</a></li>
</ol>
<li><a href="#arrunsized">Arrays with implicit size</a></li>
<li><a href="#enum">Enumeration</a></li>
<li><a href="#namespace">Namespaces</a></li>
</ol>
<li>Special types</li>
<ol>
<li><a href="#autoref">auto ref</a></li>
<ol>
<li><a href="#autorefcall">function call through auto ref</a></li>
</ol>
<li><a href="#autoarray">auto[]</a></li>
<li><a href="#typeid">typeid</a></li>
</ol>
<li>Miscellaneous</li>
<ol>
<li><a href="#characters">Characters</a></li>
<li><a href="#strings">Strings</a></li>
<ol>
<li><a href="#unescaped">Unescaped strings</a></li>
</ol>
<li><a href="#arrayinline">Inline arrays</a></li>
<ol>
<li><a href="#listcomprehension">List comprehension</a></li>
</ol>
<li><a href="#escapeseq">Escape sequences</a></li>
<li><a href="#binnum">Binary numbers</a></li>
<li><a href="#octnum">Octal numbers</a></li>
<li><a href="#hexnum">Hexadecimal numbers</a></li>
<li><a href="#nullptr">Null pointer</a></li>
</ol>
<li>Standard library</li>
<ol>
<li><a href="#std_typeinfo">std.typeinfo</a></li>
<li><a href="#std_dynamic">std.dynamic</a></li>
<li><a href="#std_gc">std.gc</a></li>
<li><a href="#std_vector">std.vector</a></li>
<li><a href="#std_list">std.list</a></li>
<li><a href="#std_hashmap">std.hashmap</a></li>
<li><a href="#std_range">std.range</a></li>
<li><a href="#std_file">std.file</a></li>
<li><a href="#std_io">std.io</a></li>
<li><a href="#std_random">std.random</a></li>
<li><a href="#std_time">std.time</a></li>
</ol>
<li>Appendix</li>
<ol>
<li><a href="#convrules">Rules applied to value types in a binary operation</a></li>
<li><a href="#oppriority">Operator priority</a></li>
<li><a href="#convimplicit">Implicit conversions</a></li>
</ol>
</ol>
<hr />
<h2 align="center">1. General</h2>
<div class="topic">
<h3><a name="basictypes">1.1 Basic types</a></h3>
<table border="1px">
<tr><th>Name</th><th>Size</th><th>Default alignment</th><th>Extra information</th></tr>
<tr><td>void</td><td>0 bytes</td><td>no alignment</td><td>only allowed as a function return value (returns nothing)</td></tr>
<tr><td>bool</td><td>1 byte</td><td>no alignment</td><td>values: false (0) or true (1)</td></tr>
<tr><td>char</td><td>1 byte</td><td>no alignment</td><td>values: -128..127</td></tr>
<tr><td>short</td><td>2 bytes</td><td>same as in C++ (usually, 2)</td><td>values: -32768..32767</td></tr>
<tr><td>int</td><td>4 bytes</td><td>same as in C++ (usually, 4)</td><td>values: -2147483648..2147483647</td></tr>
<tr><td>long</td><td>8 bytes</td><td>same as in C++ (usually 4 or 8)</td><td>values: -9223372036854775808..9223372036854775807</td></tr>
<tr><td>float</td><td>4 bytes</td><td>same as in C++ (usually, 4)</td><td>values: as per IEEE 754</td></tr>
<tr><td>double</td><td>8 bytes</td><td>same as in C++ (usually 4 or 8)</td><td>values: as per IEEE 754</td></tr>
</table>
</div>
<hr />
<div class="topic">
<h3><a name="varalign">1.2 Variable alignment</a></h3>
Default type alignment can be changed using two statements:<br />
noalign before type name will disable any default type alignment:<br />
<p class="code">
<span class="rword">noalign</span> <span class="rword">type</span> name; <span class="comment">// variable "name" of type "type" will not be aligned</span>
</p>
align(bytes) before type name will force specified alignment:<br />
<p class="code">
<span class="rword">align</span>(<span class="real">8</span>) <span class="rword">type</span> name; <span class="comment">// variable "name" of type "type" will be aligned to a 8 byte boundary.</span>
</p>
Alignment must not exceed 16 bytes.<br />
</div>
<hr />
<div class="topic">
<h3><a name="typeauto">1.3 Type inference</a></h3>
When defining a variable, type name can be replaced with a keyword "auto".<br />
In this case, type will be inferred from r-value type.<br />
Here are some examples:
<p class="code">
<span class="rword">auto</span> i = <span class="real">5</span>; <span class="comment">// i will have type int</span><br />
<span class="rword">auto</span> n = &i; <span class="comment">// n will have type int ref</span><br />
<span class="rword">int</span>[<span class="real">10</span>] arr;<br />
<span class="rword">auto</span> copy = arr; <span class="comment">// copy will have type int[10]</span>
</p>
Alignment of automatic types works just like with explicitly specified types.<br />
<br />
auto can be used as a function return type. If a function has different exit points returning different type, an error occurs.<br />
auto can be used as a function argument type as long as this arguments has a default value.<br />
<br />
Type inference also works in situations where overloaded function pointer is used. Compiler will infer needed overload in the following context:<br />
- assignment to a variable with known type<br />
- return from a function with known return type<br />
- in default function arguments<br />
- in function call argument list<br />
</div>
<hr />
<h2 align="center">2. Expressions</h2>
<div class="topic">
<h3><a name="import">2.1 import expression</a></h3>
<span class="rword">import</span> expressions allows code to import functions, classes and variables from other files.<br />
<br />
<span class="rword">import</span> expressions must be placed at the beginning of a file before any other expression or definition.<br />
A file name without extension must be specified after <span class="rword">import</span>.<br />
Also, folder name is accepted after <span class="rword">import</span> keyword, then a point '.' and another folder\file name expected.<br />
<br />
Expression examples:<br />
<p class="code">
<span class="rword">import</span> a; <span class="comment">// builds a.nc file and imports functions, classes and variables from it</span><br />
<span class="rword">import</span> d.e; <span class="comment">// builds d\e.nc file and imports functions, classes and variables from it</span><br />
</p>
</div>
<hr />
<div class="topic">
<h3><a name="typeof">2.2 typeof operator</a></h3>
<span class="rword">typeof</span>(expression) allows to get type of an expression (expression will not be evaluated at run time).<br />
<p class="code">
<span class="rword">typeof</span>(<span class="real">4</span>) is equal to specifying int.<br />
<span class="rword">typeof</span>(<span class="real">4</span> * <span class="real">0.2</span>) is equal to specifying double.<br />
</p>
<div class="subtopic">
<h3><a name="typeof_ext">2.2.1 Extended typeof expressions</a></h3>
Additional type information can be requested after a <span class="rword">typeof</span> expression.<br />
<br />
For function types following extended expressions are available:
<p class="code">
<span class="rword">typeof</span>(expression).<span class="rword">argument</span>.<span class="rword">first</span> - returns type of the first argument<br />
<span class="rword">typeof</span>(expression).<span class="rword">argument</span>.<span class="rword">last</span> - returns type of the last argument<br />
<span class="rword">typeof</span>(expression).<span class="rword">argument</span>.<span class="rword">size</span> - returns argument count<br />
<span class="rword">typeof</span>(expression).<span class="rword">argument</span>[<span class="real">N</span>] - returns argument at specified index<br />
<span class="rword">typeof</span>(expression).<span class="rword">return</span> - returns function return type<br />
</p>
For array types and reference types, the following extended expression is available:
<p class="code">
<span class="rword">typeof</span>(expression).<span class="rword">target</span> - returns the type of an array element type or a type the reference points to<br />
</p>
For array types, the following extended expression is available:
<p class="code">
<span class="rword">typeof</span>(expression).<span class="rword">arraySize</span> - returns size of an array (-1 for array with implicit size)<br />
</p>
Additional extended expressions are available for all types:
<p class="code">
<span class="rword">typeof</span>(expression).<span class="rword">isReference</span> - returns 1 if type is a reference and 0 otherwise<br />
<span class="rword">typeof</span>(expression).<span class="rword">isArray</span> - returns 1 if type is an array and 0 otherwise<br />
<span class="rword">typeof</span>(expression).<span class="rword">isFunction</span> - returns 1 if type is a function and 0 otherwise<br />
</p>
It is possible to get class typedef target type, class member type or a class constant value by writing a name after the point.<br />
<br />
Extended typeof expressions are also available for use immediately after the type name.<br />
</div>
</div>
<hr />
<div class="topic">
<h3><a name="sizeof">2.3 sizeof operator</a></h3>
<span class="rword">sizeof</span>(type) returns size of type.<br />
<span class="rword">sizeof</span>(expression) returns size of the expression type (it is equal to "<span class="rword">sizeof</span>(<span class="rword">typeof</span>(expression))")<br />
</div>
<hr />
<div class="topic">
<h3><a name="new">2.4 new expression</a></h3>
<span class="rword">new</span> expression allows to allocate memory from global heap.<br />
There are two versions of this expression - one is used to allocate classes and the second one is used to allocate arrays.<br />
<br />
Return type of "<span class="rword">new</span> type" is 'type ref'.<br />
Return type of "<span class="rword">new</span> type[N]", where N is an expression that results in a number is 'type[]'.<br />
<div class="subtopic">
<h3><a name="gc">2.4.1 Garbage collection</a></h3>
There is not implicit memory deallocation function, dynamic memory is managed by NULLC and is garbage collected.<br />
Garbage collection is aware that some pointers may point to a memory not managed by NULLC (you can pass pointers to your objects safely to NULLC) and will skip those.<br />
<br />
Some form of explicit management is exposed through <a href="#std_gc" title="std.gc module">std.gc</a> module.<br />
</div>
<div class="subtopic">
<h3><a name="constructor">2.4.2 Constructor call</a></h3>
If a single object is created, it is possible to call a type constructor in a <span class="rword">new</span> expression, by opening parenthesis and writing function arguments.<br />
<p class="code">
<span class="rword">int ref</span> x = <span class="rword">new</span></span> <span class="rword">int</span>(<span class="real">12</span>);<br />
</p>
Default class constructor will be called if parentheses are not written.<br />
To define a constructor for a type, add a member function with the name equal to the class name. Read more in a topic about <a href="#classconstructor">class constructor</a>.<br />
<br />
It is possible to define custom construction for an object by writing code inside figure braces {}:<br />
<pre class="code">
<span class="rword">class Foo</span>{ <span class="rword">int </span><span class="vardef">x</span>; }
<span class="rword">auto </span><span class="vardef">a </span>= <span class="rword">new Foo</span><span class="bold">{ </span><span class="var">x </span>= <span class="real">4</span>; <span class="bold">}</span>;
<span class="rword">auto </span><span class="vardef">b </span>= <span class="rword">new Foo</span><span class="bold">(){ </span><span class="var">x </span>= <span class="real">8</span>; <span class="bold">}</span>;
<span class="rword">return </span><span class="var">a</span>.<span class="var">x </span>+ <span class="var">b</span>.<span class="var">x</span><span class="bold">;</span><span class="comment"> // 12
</span></pre>
</div>
<div class="subtopic">
<h3><a name="finalize">2.4.3 Object finalization</a></h3>
If a class implements a "finalize" function which doesn't accept any arguments, then when GC finds that the class instance is not referenced any more, this function will be called
to perform any kind of clean-up you may want to do on it.<br />
Finalizable class has a restriction that other classes do not have: it cannot be placed on stack or be a member of some other class, only references to it or arrays with implicit size
can be created.<br />
<br />
It is strongly not recommended to create new references to an object from its finalizer because finalizer runs automatically only once per class instance.<br />
</div>
</div>
<hr />
<div class="topic">
<h3><a name="break">2.5 break expression</a></h3>
<span class="rword">break</span> expression allows to end execution of a cycle.<br />
<br />
<span class="rword">break</span>; exits current cycle.<br />
<span class="rword">break</span> <span class="real">N</span>; where N is number, known at compile time exits from N cycles beginning from where it is written.<br />
<span class="rword">break</span> <span class="real">1</span>; is equal to <span class="rword">break</span>;<br />
</div>
<hr />
<div class="topic">
<h3><a name="continue">2.6 continue expression</a></h3>
<span class="rword">continue</span> expression allows to skip to the next iteration of a cycle.<br />
<br />
<span class="rword">continue</span>; skips to the end of current cycle.<br />
<span class="rword">continue</span> <span class="real">N</span>; where N is number, known at compile time exits from (N-1) cycles beginning from where it is written and skips to the next iteration of cycle it ends up.<br />
<span class="rword">continue</span> <span class="real">1</span>; is equal to <span class="rword">continue</span>;<br />
</div>
<hr />
<div class="topic">
<h3><a name="return">2.7 return expression</a></h3>
<span class="rword">return</span> expression allows to return a value or exit from a function, or to end execution of a program if placed in global scope.<br />
<p class="code">
<span class="rword">return</span>; <span class="comment">// exits function, returning void type (nothing).</span><br />
<span class="rword">return</span> expr; <span class="comment">// exits function or ends program execution, returning result of "expr".</span><br />
</p>
<span class="rword">return</span> that is placed globally accepts only basic build-un types and cannot return void.<br />
<br />
When value is returned from function, it is converted to functions' return type if conversion is possible.<br />
If function return type is <a href="#typeauto">auto</a>, function return type is set to the type of "expr".<br />
</div>
<hr />
<div class="topic">
<h3><a name="yield">2.8 yield expression</a></h3>
<span class="rword">yield</span> expression allows to return a value (optionally) and execution to the calling function. The difference from <span class="rword">return</span> expression
is that the next time the function is called; it will continue execution from the point immediately after last <span class="rword">yield</span>.<br />
<p class="code">
<span class="rword">yield</span>; <span class="comment">// returning void type (nothing).</span><br />
<span class="rword">yield</span> expr; <span class="comment">// returning result of "expr".</span><br />
</p>
Return value conversion and <a href="#typeauto">auto</a> return type resolution is performed exactly as if <span class="rword">return</span> expression was used.<br />
<span class="rword">yield</span> can only be used inside <a href="#coroutine">coroutines</a>.
</div>
<hr />
<div class="topic">
<h3><a name="typedef">2.9 typedef expression</a></h3>
<span class="rword">typedef</span> expression allows to create one-word aliases to other types.<br />
<br />
<span class="rword">typedef</span> doesn't create new type, it just allows its target type to have different names.<br />
<br />
Examples:
<p class="code">
<span class="rword">typedef</span> <span class="rword">int</span>[<span class="real">4</span>] fourInts;<br />
<span class="rword">typedef</span> <span class="rword">int</span> <span class="rword">ref</span>(<span class="rword">int</span>, <span class="rword">int</span>) drawCallback;<br />
</p>
</div>
<hr />
<h2 align="center">3. Statements</h2>
<div class="topic">
<h3><a name="if">3.1 if statement</a></h3>
<p class="code">
<span class="rword">if</span>(<span class="var">expr</span>)<br />
<span class="var">trueBody</span><br />
<span class="rword">else</span><br />
<span class="var">falseBody</span><br />
</p>
<span class="rword">if</span> statement evaluates result of expression "expr", and if result isn't equal to 0, evaluates expressions put in "trueBody". Otherwise, it evaluates expressions put in "falseBody".<br />
"trueBody" and "falseBody" may consist of a single expression or a block of expressions.<br />
<span class="rword">else</span> and "falseBody", may be omitted. In such case, if result of "expr" is zero, nothing is evaluated.<br />
<br />
If "expr" type is a user-defined type, a conversion to bool is performed by calling bool(expr) function.<br />
<div class="subtopic">
<h4><a name="static_if">3.1.1 Static if</a></h4>
If an <span class="rword">@</span> is placed before an <span class="rword">if</span> expression, then, condition will be evaluated at compilation time (which is not always possible) and
the body will be compiled only if condition is true.<br />
This is most useful in generic functions where different code paths can be made for different type groups and not all code paths can be successfully compiled with wrong type groups.<br />
</div>
</div>
<hr />
<div class="topic">
<h3><a name="for">3.2 for statement</a></h3>
<div class="subtopic">
<h4><a name="forsimple">3.2.1 C-style for</a></h4>
<p class="code">
<span class="rword">for</span>(<span class="var">initexpr</span>; <span class="var">condexpr</span>; <span class="var">iterexpr</span>)<br />
<span class="var">body</span><br />
</p>
<span class="rword">for</span> statement is a cycle that evaluates "initexpr", and executes "body" while "condexpr" evaluates to a non-zero value.<br />
"iterexpr" is evaluated at the end of every cycle.<br />
<br />
"body", "initexpr" and "iterexpr" may consist of a single expression or a block of expressions.<br />
"condexpr" must be a single expression with result type of int\long\double.<br />
<br />
If "condexpr" type is a user-defined type, a conversion to bool is performed by calling bool(condexpr) function.<br />
</div>
<div class="subtopic">
<h4><a name="foreach">3.2.2 for each</a></h4>
A shorter form of for exists to iterate through all elements of an array, a custom iterator or a coroutine.<br />
<p class="code">
<span class="rword">for</span>(<span class="rword">type</span> <span class="var">refName</span> <span class="rword">in</span> <span class="var">expression</span>, <span class="rword">type</span> <span class="var">refNameN</span> <span class="rword">in</span> <span class="var">expressionN</span>)<br />
<span class="var">body</span><br />
</p>
<span class="rword">type</span> name is optional and should be used when iterator return type is <a href="#autoref" title="auto ref type"><span class="rword">auto ref</span></a>, to convert it to selected type.<br />
If more than one iterator is specified, loop will terminate when one of the iterators reaches its end.<br />
For example, return value of the following code is 14 (1 * 4 + 2 * 5):
<p class="code"><span class="rword">int</span> sum = 0;<br />
<span class="rword">for</span>(x <span class="rword">in</span> {1, 2, 3}, y <span class="rword">in</span> {4, 5})<br />
sum += x * y;<br />
<span class="rword">return</span> sum;<br />
</p>
It is possible to iterate through all the elements yielded by a <a href="#coroutine">coroutine</a>.<br />
But be careful, just like in a <a href="#listcomprehension">list comprehension</a>, a <a href="#return">return</a> expression will stop the iteration, ignoring the result.<br />
</div>
<div class="subtopic">
<h4><a name="foreachiternator">3.2.3 Custom iterator</a></h4>
If you wish to iterate over elements of your type or add a contextless custom iterator (for examples of those, see <a href="#std_range" title="std.range module">std.range</a> module), you must add a specific function:<br />
<p class="code">
<span class="rword">type_iterator</span> <span class="rword">type</span>:start()<br />
{<br />
<span class="comment">/* code that returns iterator */</span><br />
}
</p>
start() function will be explicitly called for expression to the right of <span class="rword">in</span> keyword.<br />
<br />
Iterator type must implement two functions:<br />
<p class="code">
<span class="rword">auto</span> <span class="rword">type_iterator</span>:hasnext()<br />
{<br />
<span class="comment">/* return 1 if there are elements left to iterate through, and 0 to end iteration */</span><br />
}
</p>
<p class="code">
<span class="rword">auto</span> <span class="rword">type_iterator</span>:next()<br />
{<br />
<span class="comment">/* code that returns current element and moves on to the next */</span><br />
}
</p>
Keep in mind that next() function should return <b>current</b> iterator element and move on to the next.<br />
<div>
<p class="example_head" id="iterator_ex_1a" onmousedown="toggleView("iterator_ex_1a", "iterator_ex_1b", "example of an iterator over elements of a single-linked list.");">Show example of an iterator over elements of a single-linked list.</p>
<p class="code example_body" id="iterator_ex_1b">
<span class="comment">// List node class contains element value and a pointer to the next element</span><br />
<span class="rword">class</span> <span class="rword">list_node</span><br />
{<br />
<span class="rword">list_node</span> <span class="rword">ref</span> next;<br />
<span class="rword">int</span> value;<br />
}<br />
<span class="comment">// list type iterator will contain pointer to the current node he is at.</span><br />
<span class="rword">class</span> <span class="rword">list_iterator</span><br />
{<br />
<span class="rword">list_node</span> <span class="rword">ref</span> curr;<br />
}<br />
<span class="comment">// Iterator initialization function</span><br />
<span class="rword">auto</span> <span class="rword">list_node</span>:<span class="func">start</span>()<br />
{<br />
<span class="comment">// Create iterator</span><br />
<span class="rword">list_iterator</span> ret;<br />
<span class="comment">// Set its start to this node</span><br />
<span class="var">ret</span>.<span class="var">curr</span> = <span class="var">this</span>;<br />
<span class="comment">// return iterator</span><br />
<span class="rword">return</span> <span class="var">ret</span>;<br />
}<br />
<span class="rword">auto</span> <span class="rword">list_iterator</span>:<span class="func">hasnext</span>()<br />
{<br />
<span class="comment">// While curent node pointer is valid, signal that there are elements left to iterate through</span><br />
<span class="rword">return</span> <span class="var">curr</span> ? <span class="real">1</span> : <span class="real">0</span>;<br />
}<br />
<span class="comment">// This function will return iterator element and move on to the next</span><br />
<span class="rword">auto</span> <span class="rword">list_iterator</span>:<span class="func">next</span>()<br />
{<br />
<span class="comment">// Save element value</span><br />
<span class="rword">int</span> ret = <span class="var">curr</span>.<span class="var">value</span>;<br />
<span class="comment">// Move to the next element</span><br />
<span class="var">curr</span> = <span class="var">curr</span>.<span class="var">next</span>;<br />
<span class="comment">// Return old element value</span><br />
<span class="rword">return</span> <span class="var">ret</span>;<br />
}<br />
<span class="comment">// Create a list of two numbers</span><br />
<span class="rword">list_node</span> list;<br />
<span class="var">list</span>.<span class="var">value</span> = <span class="real">2</span>;<br />
<span class="var">list</span>.<span class="var">next</span> = <span class="rword">new</span> <span class="rword">list_node</span>;<br />
<span class="var">list</span>.<span class="var">next</span>.<span class="var">value</span> = <span class="real">5</span>;<br />
<span class="comment">// Compute product of numbers in a list using our custom iterator</span><br />
<span class="rword">int</span> product = <span class="real">1</span>;<br />
<span class="rword">for</span>(i <span class="rword">in</span> <span class="var">list</span>)<br />
<span class="var">product</span> *= <span class="var">i</span>;<br />
<span class="rword">return</span> <span class="var">product</span>;<br />
</p>
</div>
</div>
</div>
<hr />
<div class="topic">
<h3><a name="while">3.3 while statement</a></h3>
<p class="code">
<span class="rword">while</span>(<span class="var">condexpr</span>)<br />
<span class="var">body</span><br />
</p>
<span class="rword">while</span> statement is a cycle that executes "body" while "condexpr" evaluates to a non-zero value.<br />
<br />
"body" may consist of a single expression or a block of expressions.<br />
"condexpr" must be a single expression with result type of int\long\double.<br />
<br />
If "condexpr" type is a user-defined type, a conversion to bool is performed by calling bool(condexpr) function.<br />
</div>
<hr />
<div class="topic">
<h3><a name="do">3.4 do...while statement</a></h3>
<p class="code">
<span class="rword">do</span><br />
<span class="var">body</span><br />
<span class="rword">while</span>(<span class="var">condexpr</span>)<br />
</p>
<span class="rword">do</span> statement is a cycle that executes "body" while "condexpr" evaluates to a non-zero value.<br />
Difference from while statement is that do statement evaluates body at least once, since condition is placed after the body.<br />
<br />
"body" may consist of a single expression or a block of expressions.<br />
"condexpr" must be a single expression with result type of int\long\double.<br />
<br />
If "condexpr" type is a user-defined type, a conversion to bool is performed by calling bool(condexpr) function.<br />
</div>
<hr />
<div class="topic">
<h3><a name="switch">3.5 switch statement</a></h3>
<p class="code">
<span class="rword">switch</span>(<span class="var">expr</span>)<br />
{<br />
<span class="rword">case</span> <span class="real">A</span>:<br />
<span class="var">caseBody</span>;<br />
<span class="rword">default</span>:<br />
<span class="var">defaultBody</span>;<br />
}<br />
</p>
<span class="rword">switch</span> statement evaluates result of "expr" and compares result with case label values. If result matches <span class="rword">case</span> value, a jump to a matched <span class="rword">case</span> label is made and all expressions after it are evaluated.<br />
If no case label value is equal to result, a jump to <span class="rword">default</span> label is made.<br />
<span class="rword">break</span>; expression can be used inside <span class="rword">switch</span> statement.<br />
<span class="rword">default</span> label can be omitted.<br />
<span class="rword">switch</span> without case labels, or without any expressions at all is legal.<br />
</div>
<hr />
<h2 align="center">4. Declarations</h2>
<div class="topic">
<h3><a name="classes">4.1 User classes</a></h3>
<p class="code">
<span class="rword">class</span> Name<br />
{<br />
<span class="rword">type</span> name, name, ...;<br />
<span class="rword">type</span> <span class="func">function</span>(<span class="rword">type</span> arg, <span class="rword">type</span> arg, ...){ }<br />
...<br />
}<br />
</p>
Class consists of variables, functions, typedefs' and constants in any order, but keep in mind, functions can only access members that are defined before them.<br />
<br />
Class constants are numeric type members that are defined after a <span class="rword">const</span> keyword. Such members do not take any space inside a class, and can be accessed
as other members in an extended typeof expression with a difference that their value will be resolved immediately at compilation time.<br />
Constant members must have a default value assigned to them. Assignment can be skipped for integer constants after the first one in a comma-separated list, in which case they will
be autoincremented:<br />
<pre class="code">
<span class="rword">class Foo
</span>{<span class="comment">
// A equals 5, B equals 6 and C equals 7
</span><span class="rword"> const int </span><span class="vardef">A </span>= <span class="real">5</span>, <span class="vardef">B</span>, <span class="vardef">C</span>;
}
<span class="rword">Foo </span><span class="vardef">a</span>;
<span class="rword">return Foo</span>.<span class="var">B </span>+ <span class="var">a</span>.<span class="var">C</span><span class="bold">;</span><span class="comment"> // Answer is 13. Constants can be accessed directly or by using a class instance.</span></pre>
There is no default alignment by default. To specify alignment, put "noalign" of "align(bytes)" before "class" keyword.<br />
Specifying "noalign" is superfluous. Alignment must not exceed 16 bytes.<br />
<div class="subtopic">
<h4><a name="classaccessor">4.1.1 Accessors</a></h4>
There is a possibility to add virtual class members - members that can be used as any real class member, but access and modification of them is implemented in custom functions.<br />
To add an accessor to your class, use the following syntax:<br />
<p class="code">
<span class="comment">// read-only accessor</span><br />
<span class="rword">type</span> name{ <span class="rword">get</span>{ <span class="comment">/* getter body */</span> } };<br />
<span class="comment">// accessor with read/write access</span><br />
<span class="rword">type</span> name{ <span class="rword">get</span>{ <span class="comment">/* getter body */</span> } <span class="rword">set</span>{ <span class="comment">/* setter body */</span> } };<br />
<span class="comment">// accessor with read-write access and a custom name for right-hand value of set function</span><br />
<span class="rword">type</span> name{ <span class="rword">get</span>{ <span class="comment">/* getter body */</span> } <span class="rword">set</span>(value){ <span class="comment">/* setter body */</span> } };<br />
</p>
<div class="example">
<p class="example_head" id="accessor_ex_1a" onmousedown="toggleView("accessor_ex_1a", "accessor_ex_1b", "example of a read-only accessor.");">Show example of a read-only accessor.</p>
<p class="code example_body" id="accessor_ex_1b">
<span class="comment">// Let's create a "sum" accessor to a class consisting of two numbers</span><br />
<span class="rword">class</span> <span class="rword">NumberPair</span><br />
{<br />
<span class="rword">int</span> a, b;<br />
<span class="comment">// accessor is read-only and returns sum of both members. return type can be inferred automatically</span><br />
<span class="rword">auto</span> sum{ <span class="rword">get</span>{ <span class="rword">return</span> <span class="var">a</span> + <span class="var">b</span>; } };<br />
}<br />
<span class="rword">NumberPair</span> foo;<br />
<span class="var">foo</span>.<span class="var">a</span> = <span class="real">5</span>;<br />
<span class="var">foo</span>.<span class="var">b</span> = <span class="real">10</span>;<br />
<span class="rword">return</span> <span class="var">foo</span>.<span class="var">sum</span>; <span class="comment">// returns 15</span><br />
</p>
</div>
<div class="example">
<p class="example_head" id="accessor_ex_2a" onmousedown="toggleView("accessor_ex_2a", "accessor_ex_2b", "example of a read-write accessor.");">Show example of a read-write accessor.</p>
<p class="code example_body" id="accessor_ex_2b">
<span class="comment">// Suppose we have a two-component floating point vector and we want to add a possibility for swizzling</span><br />
<span class="rword">class</span> <span class="rword">vec2</span><br />
{<br />
<span class="rword">float</span> x, y;<br />
<span class="comment">// xy swizzle is trivial</span><br />
<span class="rword">auto</span> xy<br />
{<br />
<span class="rword">get</span><br />
{<br />
<span class="rword">return</span> <span class="var">this</span>;<br />
}<br />
<span class="rword">set</span><br />
{<br />
<span class="var">x</span> = <span class="var">r</span>.<span class="var">x</span>;<br />
<span class="var">y</span> = <span class="var">r</span>.<span class="var">y</span>;<br />
}<br />
};<br />
<span class="comment">// in yx swizzle, a temporary vector should be created</span><br />
<span class="rword">auto</span> yx<br />
{<br />
<span class="rword">get</span><br />
{<br />
<span class="rword">vec2</span> tmp;<br />
<span class="var">tmp</span>.<span class="var">x</span> = <span class="var">y</span>;<br />
<span class="var">tmp</span>.<span class="var">y</span> = <span class="var">x</span>;<br />
<span class="rword">return</span> <span class="var">tmp</span>;<br />
}<br />
<span class="rword">set</span>(value)<br />
{<br />
<span class="var">y</span> = <span class="var">value</span>.<span class="var">x</span>;<br />
<span class="var">x</span> = <span class="var">value</span>.<span class="var">y</span>;<br />
}<br />
};<br />
}<br />
<span class="rword">vec2</span> foo;<br />
<span class="var">foo</span>.<span class="var">x</span> = <span class="real">1.5</span>;<br />
<span class="var">foo</span>.<span class="var">y</span> = <span class="real">2.5</span>;<br />
<span class="rword">vec2</span> bar;<br />
<span class="var">bar</span>.<span class="var">yx</span> = <span class="var">foo</span>.<span class="var">xy</span>;<br />
<span class="rword">return</span> <span class="var">foo</span>.<span class="var">x</span> * <span class="var">bar</span>.<span class="var">y</span>; <span class="comment">// returns 2.25</span><br />
</p>
</div>
It is possible to add accessors to a class outside of a class definition. In this case, getter and setter are defined separately.<br />
A syntax is similar to external <a href="#funcmember">member function definition</a>, only '.' is used instead of ':':<br />
<p class="code">
<span class="comment">// Second accessor of "read-write accessor" example above defined outside of a class</span><br />
<span class="rword">auto</span> <span class="rword">vec2</span>.<span class="func">yx</span>()<br />
{<br />
<span class="rword">vec2</span> tmp;<br />
<span class="var">tmp</span>.<span class="var">x</span> = <span class="var">y</span>;<br />
<span class="var">tmp</span>.<span class="var">y</span> = <span class="var">x</span>;<br />
<span class="rword">return</span> <span class="var">tmp</span>;<br />
}<br />
<span class="rword">void</span> <span class="rword">vec2</span>.<span class="func">yx</span>(<span class="rword">vec2</span> <span class="rword">ref</span> value)<br />
{<br />
<span class="var">y</span> = <span class="var">value</span>.<span class="var">x</span>;<br />
<span class="var">x</span> = <span class="var">value</span>.<span class="var">y</span>;<br />
}<br />
</p>
</div>
<div class="subtopic">
<h4><a name="classgeneric">4.1.2 Generic classes</a></h4>
Generic class is a class that depends on type arguments, a single definition of which can be instanced with different arguments to produce different types.<br />
Type argument list is a list of aliases to a to-be-defined-later types inside an angle-brackets '<' '>':<br />
<pre class="code">
<span class="rword">class Pair</span><<span class="rword">T</span>, <span class="rword">U</span>>
{
<span class="rword">T </span><span class="vardef">first</span>;
<span class="rword">U </span><span class="vardef">second</span>;
} </pre>
This is a generic type definition, and it doesn't create any real types yet.<br />
Required type is instanced later, when generic type name is followed with a type list in angle-brackets:<br />
<pre class="code">
<span class="rword">Pair</span><<span class="rword">int</span>, <span class="rword">float</span>> <span class="vardef">a</span>;
<span class="rword">Pair</span><<span class="rword">double</span>, <span class="rword">long</span>> <span class="vardef">b</span>;</pre>
In this example, variables "a" and "b" have different types.<br />
Nesting generic types are also allowed:
<pre class="code">
<span class="rword">Pair</span><<span class="rword">Pair</span><<span class="rword">int</span>, <span class="rword">int</span>>, <span class="rword">Pair</span><<span class="rword">double</span>, <span class="rword">double</span>>> <span class="vardef">c</span>;</pre>
<div class="subtopic">
<h4><a name="classgenericspec">4.1.2.1 Generic class member type specialization</a></h4>
It is possible to define a special member function implementation for a single instanced type by specifying full type name in an external member function definition:<br />
<pre class="code">
<span class="rword">import </span><span class="var">std</span>.<span class="var">math</span>;<span class="comment">
// define a tuple of two values
</span><span class="rword">class Pair</span><<span class="rword">T</span>, <span class="rword">U</span>>
{
<span class="rword"> T </span><span class="vardef">first</span>;
<span class="rword"> U </span><span class="vardef">second</span>;
}<span class="comment">
// Helper function that returns a Pair object with type according to arguments
</span><span class="rword">auto </span><span class="func">Pair</span><span class="bold">(</span>@<span class="rword">T </span><span class="var">a</span>, @<span class="rword">U </span><span class="var">b</span><span class="bold">){ </span><span class="rword">Pair</span><<span class="rword">T</span>, <span class="rword">U</span>> <span class="vardef">p</span>; <span class="var">p</span>.<span class="var">first </span>= <span class="var">a</span>; <span class="var">p</span>.<span class="var">second </span>= <span class="var">b</span>; <span class="rword">return </span><span class="var">p</span><span class="bold">; }</span><span class="comment">
// Function compares first with second
</span><span class="rword">bool Pair</span><span class="bold">:</span><span class="func">Compare</span><span class="bold">(){ </span><span class="rword">return </span><span class="var">first </span>> <span class="var">second</span><span class="bold">; }</span><span class="comment">
// Specialization for pair of float2 compares vector lengths
</span><span class="rword">bool Pair</span><<span class="rword">float2</span>, <span class="rword">float2</span>><span class="bold">:</span><span class="func">Compare</span><span class="bold">(){ </span><span class="rword">return </span><span class="var">first</span>.<span class="func">length</span><span class="bold">() </span>> <span class="var">second</span>.<span class="func">length</span><span class="bold">(); }</span><span class="comment">
// Create a pair
</span><span class="rword">auto </span><span class="vardef">p </span>= <span class="func">Pair</span><span class="bold">(</span><span class="func">float2</span><span class="bold">(</span><span class="real">4</span>, <span class="real">4</span><span class="bold">)</span>, <span class="func">float2</span><span class="bold">(</span><span class="real">1</span>, <span class="real">5</span><span class="bold">))</span>;<span class="comment">
// Compare values
</span><span class="rword">return </span><span class="var">p</span>.<span class="func">Compare</span><span class="bold">();</span><span class="comment"> // true</span></pre>
</div>
</div>
<div class="subtopic">
<h4><a name="classconstructor">4.1.3 Class constructor</a></h4>
A class member function that has the same name as the type is considered to be a <b>class constructor</b>.<br />
As described in a section about <a href="#constructor">new expression</a>, class constructors are functions that can be called after object allocation.<br />
But because an object can be placed on stack instead of heap memory, compiler will call a default constructor for variables that are placed on stack.<br />
Default constructor is simply a constructor that can be called without any arguments (constructor can have <a href="#funcdefarg">default function arguments</a>).<br />
<pre class="code">
<span class="rword">class Foo
</span>{
<span class="rword"> int </span><span class="vardef">x</span>;
<span class="rword"> void </span><span class="func">Foo</span><span class="bold">(){ </span><span class="var">x </span>= <span class="real">12</span>; <span class="bold">}
</span>}
<span class="rword">Foo </span><span class="vardef">a</span>;
<span class="rword">return </span><span class="var">a</span>.<span class="var">x</span><span class="bold">;</span><span class="comment"> // 12</span></pre>
If a type has members with default constructor, but the type itself doesn't have a user-defined default constructor, the compiler will create a default constructor that
will be used until a user-defined default constructor is created.<br />
Moreover, class member default constructors will be called at the beginning of a user-defined constructor.<br />
<pre class="code">
<span class="rword">class Foo
</span>{
<span class="rword"> int </span><span class="vardef">x</span>;
<span class="rword"> void </span><span class="func">Foo</span><span class="bold">(){ </span><span class="var">x </span>= <span class="real">12</span>; <span class="bold">}
</span>}
<span class="rword">class Bar
</span>{
<span class="rword">int </span><span class="vardef">y</span>;
<span class="rword">Foo </span><span class="vardef">z</span>;
<span class="rword"> void </span><span class="func">Bar</span><span class="bold">(){ </span><span class="var">y </span>= <span class="real">20</span>; <span class="bold">}</span><span class="comment"> // default Foo constructor is called automatically
</span>}
<span class="rword">Bar </span><span class="vardef">a</span>;
<span class="rword">return </span><span class="var">a</span>.<span class="var">z</span>.<span class="var">x </span>* <span class="var">a</span>.<span class="var">y</span><span class="bold">;</span><span class="comment"> // 240</span></pre>
Class constructor can be called as a global function. In this case, class constructor return type is ignored and constructor call results in a new temporary object on stack:<br />
<pre class="code">
<span class="rword">class Foo
</span>{
<span class="rword"> int </span><span class="vardef">x</span>;
<span class="rword"> void </span><span class="func">Foo</span><span class="bold">(</span><span class="rword">int </span><span class="var">x</span><span class="bold">){ </span><span class="var">this</span>.<span class="var">x </span>= <span class="var">x</span>; <span class="bold">}
</span>}
<span class="rword">Foo </span><span class="vardef">a </span>= <span class="func">Foo</span><span class="bold">(</span><span class="real">5</span><span class="bold">)</span>;
<span class="rword">return </span><span class="var">a</span>.<span class="var">x</span><span class="bold">;</span><span class="comment"> // 5</span></pre>
</div>
<div class="subtopic">
<h4><a name="classforward">4.1.4 Class forward declaration</a></h4>
It is possible to define a type name without defining internal class structure. Such definitions are called forward declarations.<br />
A forward-declared type is limited in how it is used: only references to it or arrays with implicit size can be defined until the full class definition.<br />
<pre class="code">
<span class="rword">class List</span>;
<span class="rword">class Node
</span>{
<span class="rword">int </span><span class="vardef">value</span>;
<span class="rword">Node ref </span><span class="vardef">next</span>;
<span class="rword">List ref </span><span class="vardef">parent</span>;
}
<span class="rword">class List
</span>{
<span class="rword">Node ref </span><span class="vardef">first</span>;
}</pre>
It is not possible to create a forward declaration for a generic class.
</div>
</div>
<hr />
<div class="topic">
<h3><a name="vardef">4.2 Variables</a></h3>
Simple variable definition syntax is:
<p class="code">
<span class="rword">type</span> name;
</p>
Arrays are defined like this:
<p class="code">
<span class="rword">type</span>[<span class="real">N</span>] name;
</p>
or
<p class="code">
<span class="rword">type</span>[] name;
</p>
If array size is specified, it must be a number known at compile type.<br />
Second declaration is described in section <a href="#arrunsized">"Arrays with implicit size"</a>.<br />
<br />
Pointers are defined by using "ref" keyword:
<p class="code">
<span class="rword">type</span> <span class="rword">ref</span> name;
</p>
Pointers to functions are defined by using "ref" keyword, followed by a list of function argument types in parenthesis:
<p class="code">
<span class="rword">type</span> <span class="rword">ref</span>(<span class="rword">type</span>, <span class="rword">type</span>) name;
</p>
More than one variable can be defined in one statement, by specifying variable names after a comma.<br />
All variables defined in one statement have equal type.<br />
Different type modificators can be mixed together to form types like, for example:
<p class="code">
<span class="rword">int</span>[<span class="real">4</span>][<span class="real">2</span>] name; <span class="comment">// array of 4 arrays of two integers</span><br />
<span class="rword">int</span> <span class="rword">ref</span>(<span class="rword">int</span>, <span class="rword">int</span>)[<span class="real">4</span>] name; <span class="comment">// array of 4 pointers to functions, returning int</span>
</p>
A value can be assigned to a variable in place of its definition, by writing "= value" after its name.<br />
If array element is assigned to a type, then it will be a default value of all array elements.<br />
</div>
<hr />
<div class="topic">
<h3><a name="functions">4.3 Functions</a></h3>
Global functions, nested (local) functions, member functions and function literals are supported.<br />
<br />
Function definition starts with a function return type, followed by name and a list of arguments in parenthesis.<br />
List of arguments starts with a type that is followed by one or more parameter names, separated by comma. After a comma, a different type can be selected as well.<br />
Function body must be enclosed in { } block.<br />
<p class="code">
<span class="rword">type</span> <span class="func">name</span>(<span class="rword">type</span> a, b, <span class="rword">type</span> c, d){ }
</p>
Function return type can be any type expression including <span class="rword">auto</span>.<br />
Function argument type can be auto only if it has default argument value.<br />
<a name="funcdefarg"></a>Function default argument value is specified by writing "= value" directly after argument name:<br />
<p class="code">
<span class="rword">auto</span> <span class="func">function</span>(<span class="rword">int</span> a = <span class="real">5</span>, <span class="auto">int</span> b = <span class="real">5.0</span>){ }
</p>
After first argument with a default value, the following arguments must all have a default value.<br />
<hr />
<div class="subtopic">
<h4><a name="varargs">4.3.1 Variable argument list</a></h4>
It is possible to create a function that accepts an unspecified number of arguments (like using an ellipsis in C++).<br />
To do so, set the type of the last argument of your function to <span class="rword">auto ref</span>[].<br />
All excessive arguments will be packed into the <span class="rword">auto ref</span> array, so an <a href="#auterefcast" title="auto ref type explicit cast">explicit cast</a> is needed to convert it to the type you want.<br />
<div class="example">
<p class="example_head" id="varargs_ex_1a" onmousedown="toggleView("varargs_ex_1a", "varargs_ex_1b", "example of sum of integers using function with variable arguments.");">Show example of sum of integers using function with variable arguments.</p>
<p class="code example_body" id="varargs_ex_1b">
<span class="comment">// A function that returns sum of all its arguments. Function expects integers.</span><br />
<span class="rword">int</span> <span class="func">sum</span>(<span class="rword">auto ref</span>[] args)<br />
{<br />
<span class="rword">int</span> result = <span class="real">0</span>;<br />
<span class="comment">// iterate through all arguments and sum them</span><br />
<span class="rword">for</span>(i <span class="rword">in</span> <span class="var">args</span>)<br />
<span class="var">result</span> += <span class="rword">int</span>(<span class="var">i</span>);<br />
<span class="rword">return</span> <span class="var">result</span>;<br />
}<br />
<span class="rword">return</span> <span class="func">sum</span>(<span class="real">1</span>, <span class="real">12</span>, <span class="real">201</span>); <span class="comment">// 214</span><br />
</p>
</div>
Argument type can be found using <span class="func">typeid</span>() function.<br />
<div class="example">
<p class="example_head" id="varargs_ex_2a" onmousedown="toggleView("varargs_ex_2a", "varargs_ex_2b", "example of println function.");">Show example of println function.</p>
<p class="code example_body" id="varargs_ex_2b">
<span class="comment">// Import module that enables output to console.</span><br />
<span class="rword">import</span> <span class="var">std</span>.<span class="var">io</span>;<br />
<span class="comment">// Function returns number of printed arguments</span><br />
<span class="rword">int</span> <span class="func">println</span>(<span class="rword">auto ref</span>[] args)<br />
{<br />
<span class="comment">// Assume we are able to print all arguments</span><br />
<span class="rword">int</span> printedArgs = <span class="var">args</span>.<span class="var">size</span>;<br />
<span class="comment">// iterate through all arguments</span><br />
<span class="rword">for</span>(i <span class="rword">in</span> <span class="var">args</span>)<br />
{<br />
<span class="comment">// switch by argument type</span><br />
<span class="rword">switch</span>(<span class="func">typeid</span>(<span class="var">i</span>))<br />
{<br />
<span class="rword">case int</span>:<br />
<span class="var">io</span>.<span class="var">out</span> << <span class="func">int</span>(<span class="var">i</span>);<br />
<span class="rword">break</span>;<br />
<span class="rword">case double</span>:<br />
<span class="var">io</span>.<span class="var">out</span> << <span class="func">double</span>(<span class="var">i</span>);<br />
<span class="rword">break</span>;<br />
<span class="rword">case char</span>[]:<br />
<span class="var">io</span>.<span class="var">out</span> << <span class="rword">char</span>[](<span class="var">i</span>);<br />
<span class="rword">break</span>;<br />
<span class="comment">// unknown type, unable to print it, so we decrement printed argument count</span><br />
<span class="rword">default</span>:<br />
<span class="var">printedArgs</span>--;<br />
}<br />
}<br />
<span class="comment">// Add newline</span><br />
<span class="var">io</span>.<span class="var">out</span> << <span class="var">io</span>.<span class="var">endl</span>;<br />
<span class="comment">// Return printed argument count</span><br />
<span class="rword">return</span> <span class="var">printedArgs</span>;<br />
}<br />
<span class="rword">return</span> <span class="func">println</span>(<span class="real">2</span>, " ", <span class="real">4</span>, "<span class="string"> hello </span>", <span class="real">5.0</span>, <span class="real">3.0f</span>); <span class="comment">// output "2 4 hello 5.0"; 5 is returned</span><br />
</p>
</div>
</div>
<hr />
<div class="subtopic">
<h4><a name="funclocal">4.3.2 Local functions</a></h4>
Functions can be defined within other functions.<br />
Pointers to such functions remain valid after parent function ends.<br />
</div>
<hr />
<div class="subtopic">