-
Notifications
You must be signed in to change notification settings - Fork 6
/
conditional-compilation.html
961 lines (910 loc) · 58.2 KB
/
conditional-compilation.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
<!DOCTYPE html>
<html lang="en" data-theme="dark">
<head>
<meta charset="UTF-8">
<meta content="width=device-width, initial-scale=1" name="viewport">
<link rel="stylesheet" href="assets/styles/style.css">
<link rel="stylesheet" href="assets/styles/vlang-playground.css">
<title>V Documentation</title>
<style>
.subtopic {
margin-left: 20px !important;
}
</style>
</head>
<body>
<header>
<div class="header-container">
<ul class="header-container-left-section">
<li class="aside-topics-open">
<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 -960 960 960">
<path d="M120-240v-80h720v80H120Zm0-200v-80h720v80H120Zm0-200v-80h720v80H120Z" fill="white" />
</svg>
</li>
</ul>
<div class="logo"></div>
<ul class="header-container-right-section">
<li>
<a href="https://github.com/vlang/docs/tree/generator" target="_blank">Source code</a>
</li>
<li class="js-change-theme__action change-theme">
<svg class="theme-icon sun" width="30" height="30" viewBox="0 0 30 30" fill="none"
xmlns="http://www.w3.org/2000/svg">
<!-- <g clip-path="url(#clip0_4_47)"> -->
<path
d="M14.9854 1.92059C14.7382 1.92445 14.5026 2.02624 14.3304 2.20361C14.1581 2.38099 14.0633 2.61946 14.0667 2.86668V5.66668C14.0649 5.79036 14.0878 5.91315 14.1339 6.02792C14.18 6.14269 14.2485 6.24715 14.3353 6.33523C14.4222 6.42331 14.5256 6.49325 14.6398 6.54098C14.7539 6.58872 14.8763 6.61331 15 6.61331C15.1237 6.61331 15.2462 6.58872 15.3603 6.54098C15.4744 6.49325 15.5778 6.42331 15.6647 6.33523C15.7515 6.24715 15.82 6.14269 15.8661 6.02792C15.9122 5.91315 15.9351 5.79036 15.9333 5.66668V2.86668C15.935 2.74181 15.9117 2.61786 15.8646 2.50219C15.8176 2.38651 15.7478 2.28145 15.6594 2.19323C15.571 2.10501 15.4658 2.03542 15.35 1.98859C15.2343 1.94176 15.1103 1.91863 14.9854 1.92059ZM6.41042 5.47892C6.2249 5.47933 6.04372 5.53501 5.88999 5.63885C5.73626 5.7427 5.61696 5.89 5.54732 6.06195C5.47768 6.2339 5.46086 6.4227 5.499 6.60425C5.53714 6.7858 5.62852 6.95187 5.76146 7.08126L7.74115 9.06095C7.82715 9.15052 7.93016 9.22204 8.04415 9.2713C8.15814 9.32056 8.28081 9.34659 8.40498 9.34785C8.52915 9.34912 8.65232 9.32559 8.76728 9.27865C8.88225 9.23172 8.98669 9.16231 9.0745 9.07451C9.1623 8.9867 9.23171 8.88226 9.27864 8.76729C9.32558 8.65233 9.34911 8.52915 9.34784 8.40498C9.34658 8.28081 9.32056 8.15814 9.27129 8.04416C9.22203 7.93017 9.15051 7.82716 9.06094 7.74116L7.08125 5.76147C6.99406 5.67184 6.88975 5.60065 6.77451 5.55211C6.65928 5.50357 6.53546 5.47868 6.41042 5.47892V5.47892ZM23.5604 5.47892C23.3179 5.48614 23.0878 5.58748 22.9188 5.76147L20.9391 7.74116C20.8495 7.82716 20.778 7.93017 20.7287 8.04416C20.6795 8.15814 20.6534 8.28081 20.6522 8.40498C20.6509 8.52915 20.6744 8.65233 20.7214 8.76729C20.7683 8.88225 20.8377 8.9867 20.9255 9.0745C21.0133 9.16231 21.1178 9.23171 21.2327 9.27865C21.3477 9.32558 21.4709 9.34911 21.595 9.34785C21.7192 9.34659 21.8419 9.32056 21.9559 9.2713C22.0698 9.22203 22.1729 9.15052 22.2589 9.06095L24.2385 7.08126C24.3734 6.95016 24.4655 6.78138 24.5028 6.59703C24.5401 6.41268 24.5209 6.22136 24.4476 6.04814C24.3742 5.87493 24.2503 5.7279 24.092 5.62633C23.9337 5.52475 23.7484 5.47337 23.5604 5.47892ZM15 8.46668C13.2673 8.46668 11.6055 9.15501 10.3802 10.3802C9.155 11.6055 8.46667 13.2673 8.46667 15C8.46667 16.7328 9.155 18.3945 10.3802 19.6198C11.6055 20.845 13.2673 21.5333 15 21.5333C16.7328 21.5333 18.3945 20.845 19.6198 19.6198C20.845 18.3945 21.5333 16.7328 21.5333 15C21.5333 13.2673 20.845 11.6055 19.6198 10.3802C18.3945 9.15501 16.7328 8.46668 15 8.46668V8.46668ZM2.86667 14.0667C2.74299 14.0649 2.6202 14.0878 2.50543 14.1339C2.39066 14.18 2.2862 14.2485 2.19812 14.3353C2.11004 14.4222 2.0401 14.5257 1.99237 14.6398C1.94463 14.7539 1.92004 14.8763 1.92004 15C1.92004 15.1237 1.94463 15.2462 1.99237 15.3603C2.0401 15.4744 2.11004 15.5779 2.19812 15.6647C2.2862 15.7515 2.39066 15.82 2.50543 15.8661C2.6202 15.9122 2.74299 15.9351 2.86667 15.9333H5.66667C5.79035 15.9351 5.91314 15.9122 6.02791 15.8661C6.14268 15.82 6.24714 15.7515 6.33522 15.6647C6.4233 15.5779 6.49324 15.4744 6.54098 15.3603C6.58871 15.2462 6.6133 15.1237 6.6133 15C6.6133 14.8763 6.58871 14.7539 6.54098 14.6398C6.49324 14.5257 6.4233 14.4222 6.33522 14.3353C6.24714 14.2485 6.14268 14.18 6.02791 14.1339C5.91314 14.0878 5.79035 14.0649 5.66667 14.0667H2.86667ZM24.3333 14.0667C24.2097 14.0649 24.0869 14.0878 23.9721 14.1339C23.8573 14.18 23.7529 14.2485 23.6648 14.3353C23.5767 14.4222 23.5068 14.5257 23.459 14.6398C23.4113 14.7539 23.3867 14.8763 23.3867 15C23.3867 15.1237 23.4113 15.2462 23.459 15.3603C23.5068 15.4744 23.5767 15.5779 23.6648 15.6647C23.7529 15.7515 23.8573 15.82 23.9721 15.8661C24.0869 15.9122 24.2097 15.9351 24.3333 15.9333H27.1333C27.257 15.9351 27.3798 15.9122 27.4946 15.8661C27.6093 15.82 27.7138 15.7515 27.8019 15.6647C27.89 15.5779 27.9599 15.4744 28.0076 15.3603C28.0554 15.2462 28.08 15.1237 28.08 15C28.08 14.8763 28.0554 14.7539 28.0076 14.6398C27.9599 14.5257 27.89 14.4222 27.8019 14.3353C27.7138 14.2485 27.6093 14.18 27.4946 14.1339C27.3798 14.0878 27.257 14.0649 27.1333 14.0667H24.3333ZM8.38282 20.6565C8.14034 20.6637 7.9102 20.7651 7.74115 20.9391L5.76146 22.9188C5.67189 23.0048 5.60038 23.1078 5.55111 23.2218C5.50185 23.3357 5.47582 23.4584 5.47456 23.5826C5.4733 23.7068 5.49683 23.8299 5.54376 23.9449C5.5907 24.0599 5.6601 24.1643 5.74791 24.2521C5.83572 24.3399 5.94016 24.4093 6.05512 24.4563C6.17009 24.5032 6.29326 24.5267 6.41743 24.5255C6.5416 24.5242 6.66427 24.4982 6.77825 24.4489C6.89224 24.3996 6.99525 24.3281 7.08125 24.2386L9.06094 22.2589C9.19581 22.1278 9.28793 21.959 9.32522 21.7746C9.36252 21.5903 9.34325 21.399 9.26995 21.2257C9.19664 21.0525 9.07272 20.9055 8.91442 20.8039C8.75612 20.7024 8.57082 20.651 8.38282 20.6565ZM21.5898 20.6565C21.4042 20.6566 21.2227 20.712 21.0687 20.8157C20.9147 20.9194 20.7951 21.0667 20.7253 21.2387C20.6554 21.4107 20.6384 21.5997 20.6765 21.7814C20.7146 21.9631 20.806 22.1294 20.9391 22.2589L22.9188 24.2386C23.0048 24.3281 23.1078 24.3996 23.2218 24.4489C23.3357 24.4982 23.4584 24.5242 23.5826 24.5254C23.7067 24.5267 23.8299 24.5032 23.9449 24.4562C24.0598 24.4093 24.1643 24.3399 24.2521 24.2521C24.3399 24.1643 24.4093 24.0599 24.4562 23.9449C24.5032 23.8299 24.5267 23.7068 24.5254 23.5826C24.5242 23.4584 24.4982 23.3357 24.4489 23.2218C24.3996 23.1078 24.3281 23.0048 24.2385 22.9188L22.2589 20.9391C22.1719 20.8497 22.0679 20.7786 21.953 20.7301C21.8381 20.6815 21.7146 20.6565 21.5898 20.6565V20.6565ZM14.9854 23.3873C14.7382 23.3911 14.5026 23.4929 14.3304 23.6703C14.1581 23.8477 14.0633 24.0861 14.0667 24.3333V27.1333C14.0649 27.257 14.0878 27.3798 14.1339 27.4946C14.18 27.6094 14.2485 27.7138 14.3353 27.8019C14.4222 27.89 14.5256 27.9599 14.6398 28.0077C14.7539 28.0554 14.8763 28.08 15 28.08C15.1237 28.08 15.2462 28.0554 15.3603 28.0077C15.4744 27.9599 15.5778 27.89 15.6647 27.8019C15.7515 27.7138 15.82 27.6094 15.8661 27.4946C15.9122 27.3798 15.9351 27.257 15.9333 27.1333V24.3333C15.935 24.2085 15.9117 24.0845 15.8646 23.9689C15.8176 23.8532 15.7478 23.7481 15.6594 23.6599C15.571 23.5717 15.4658 23.5021 15.35 23.4553C15.2343 23.4084 15.1103 23.3853 14.9854 23.3873V23.3873Z"
fill="white" />
<!-- </g> -->
<defs>
<clipPath id="clip0_4_47">
<rect width="24" height="24" fill="white" transform="translate(1 1)" />
</clipPath>
</defs>
</svg>
<svg class="theme-icon moon" width="30" height="30" viewBox="0 0 30 30" fill="none"
xmlns="http://www.w3.org/2000/svg">
<path fill-rule="evenodd" clip-rule="evenodd"
d="M27.1371 20.5912C25.7519 21.0833 24.2605 21.3512 22.7065 21.3512C15.3985 21.3512 9.47424 15.4269 9.47424 8.11889C9.47424 6.10409 9.92454 4.19447 10.73 2.48517C5.60094 4.30725 1.92825 9.20347 1.92825 14.9575C1.92825 22.2655 7.85255 28.1898 15.1605 28.1898C20.4537 28.1898 25.021 25.0818 27.1371 20.5912Z"
fill="white" />
</svg>
</li>
</ul>
</div>
</header>
<div class="wrapper">
<aside class="aside-topics">
<div id="searchbox">
<!-- Search Input and Button -->
<input type="text" id="searchInput" placeholder="Search">
<button id="searchButton">
<svg width="14" height="14" viewBox="0 0 20 20" aria-hidden="true">
<path d="M14.386 14.386l4.0877 4.0877-4.0877-4.0877c-2.9418 2.9419-7.7115 2.9419-10.6533 0-2.9419-2.9418-2.9419-7.7115 0-10.6533 2.9418-2.9419 7.7115-2.9419 10.6533 0 2.9419 2.9418 2.9419 7.7115 0 10.6533z" stroke="currentColor" fill="none" fill-rule="evenodd" stroke-linecap="round" stroke-linejoin="round"></path>
</svg>
</button>
<kbd id="searchKeys"></kbd>
<div id="searchResults"></div>
</div>
<div class="title">
<a href="index.html">
<img src="assets/images/logo.svg" alt="">
<span>Documentation</span>
</a>
<div class="aside-topics-close">
<svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 -960 960 960">
<path
d="m256-200-56-56 224-224-224-224 56-56 224 224 224-224 56 56-224 224 224 224-56 56-224-224-224 224Z"
fill="white" />
</svg>
</div>
</div>
<nav>
<div class="group">
<section>
<div class="items">
<a class="link" href="introduction.html">Introduction</a>
<a class="link" href="installing-v-from-source.html">Installing V from source</a>
<a class="link" href="upgrading-v-to-latest-version.html">Upgrading V to latest version</a>
<a class="link" href="packaging-v-for-distribution.html">Packaging V for distribution</a>
<a class="link" href="getting-started.html">Getting started</a>
<a class="link" href="hello-world.html">Hello World</a>
<a class="link" href="running-a-project-folder-with-several-files.html">Running a project folder with several files</a>
<a class="link" href="comments.html">Comments</a>
<a class="link" href="functions.html">Functions</a>
<a class="link subtopic" href="functions.html#hoisting"> Hoisting</a>
<a class="link subtopic" href="functions.html#returning-multiple-values"> Returning multiple values</a>
<a class="link" href="symbol-visibility.html">Symbol visibility</a>
<a class="link" href="variables.html">Variables</a>
<a class="link subtopic" href="variables.html#mutable-variables"> Mutable variables</a>
<a class="link subtopic" href="variables.html#initialization-vs-assignment"> Initialization vs assignment</a>
<a class="link subtopic" href="variables.html#warnings-and-declaration-errors"> Warnings and declaration errors</a>
<a class="link" href="v-types.html">V Types</a>
<a class="link subtopic" href="v-types.html#primitive-types"> Primitive types</a>
<a class="link subtopic" href="v-types.html#strings"> Strings</a>
<a class="link subtopic" href="v-types.html#runes"> Runes</a>
<a class="link subtopic" href="v-types.html#numbers"> Numbers</a>
<a class="link subtopic" href="v-types.html#arrays"> Arrays</a>
<a class="link subtopic" href="v-types.html#fixed-size-arrays"> Fixed size arrays</a>
<a class="link subtopic" href="v-types.html#maps"> Maps</a>
<a class="link subtopic" href="v-types.html#map-update-syntax"> Map update syntax</a>
<a class="link" href="module-imports.html">Module imports</a>
<a class="link subtopic" href="module-imports.html#selective-imports"> Selective imports</a>
<a class="link subtopic" href="module-imports.html#module-hierarchy"> Module hierarchy</a>
<a class="link subtopic" href="module-imports.html#module-import-aliasing"> Module import aliasing</a>
<a class="link" href="statements-&-expressions.html">Statements & expressions</a>
<a class="link subtopic" href="statements-&-expressions.html#if"> If</a>
<a class="link subtopic" href="statements-&-expressions.html#match"> Match</a>
<a class="link subtopic" href="statements-&-expressions.html#in-operator"> In operator</a>
<a class="link subtopic" href="statements-&-expressions.html#for-loop"> For loop</a>
<a class="link subtopic" href="statements-&-expressions.html#defer"> Defer</a>
<a class="link subtopic" href="statements-&-expressions.html#goto"> Goto</a>
<a class="link" href="structs.html">Structs</a>
<a class="link subtopic" href="structs.html#heap-structs"> Heap structs</a>
<a class="link subtopic" href="structs.html#default-field-values"> Default field values</a>
<a class="link subtopic" href="structs.html#required-fields"> Required fields</a>
<a class="link subtopic" href="structs.html#short-struct-literal-syntax"> Short struct literal syntax</a>
<a class="link subtopic" href="structs.html#struct-update-syntax"> Struct update syntax</a>
<a class="link subtopic" href="structs.html#trailing-struct-literal-arguments"> Trailing struct literal arguments</a>
<a class="link subtopic" href="structs.html#access-modifiers"> Access modifiers</a>
<a class="link subtopic" href="structs.html#anonymous-structs"> Anonymous structs</a>
<a class="link subtopic" href="structs.html#static-type-methods"> Static type methods</a>
<a class="link subtopic" href="structs.html#noinit-structs"> [noinit] structs</a>
<a class="link subtopic" href="structs.html#methods"> Methods</a>
<a class="link subtopic" href="structs.html#embedded-structs"> Embedded structs</a>
<a class="link" href="unions.html">Unions</a>
<a class="link" href="functions-2.html">Functions 2</a>
<a class="link subtopic" href="functions-2.html#immutable-function-args-by-default"> Immutable function args by default</a>
<a class="link subtopic" href="functions-2.html#mutable-arguments"> Mutable arguments</a>
<a class="link subtopic" href="functions-2.html#variable-number-of-arguments"> Variable number of arguments</a>
<a class="link subtopic" href="functions-2.html#anonymous-&-higher-order-functions"> Anonymous & higher order functions</a>
<a class="link subtopic" href="functions-2.html#closures"> Closures</a>
<a class="link subtopic" href="functions-2.html#parameter-evaluation-order"> Parameter evaluation order</a>
<a class="link" href="references.html">References</a>
<a class="link" href="constants.html">Constants</a>
<a class="link subtopic" href="constants.html#required-module-prefix"> Required module prefix</a>
<a class="link" href="builtin-functions.html">Builtin functions</a>
<a class="link subtopic" href="builtin-functions.html#println"> println</a>
<a class="link subtopic" href="builtin-functions.html#printing-custom-types"> Printing custom types</a>
<a class="link subtopic" href="builtin-functions.html#dumping-expressions-at-runtime"> Dumping expressions at runtime</a>
<a class="link" href="modules.html">Modules</a>
<a class="link subtopic" href="modules.html#create-modules"> Create modules</a>
<a class="link subtopic" href="modules.html#special-considerations-for-project-folders"> Special considerations for project folders</a>
<a class="link subtopic" href="modules.html#init-functions"> init functions</a>
<a class="link subtopic" href="modules.html#cleanup-functions"> cleanup functions</a>
<a class="link" href="type-declarations.html">Type Declarations</a>
<a class="link subtopic" href="type-declarations.html#type-aliases"> Type aliases</a>
<a class="link subtopic" href="type-declarations.html#enums"> Enums</a>
<a class="link subtopic" href="type-declarations.html#function-types"> Function Types</a>
<a class="link subtopic" href="type-declarations.html#interfaces"> Interfaces</a>
<a class="link subtopic" href="type-declarations.html#sum-types"> Sum types</a>
<a class="link subtopic" href="type-declarations.html#optionresult-types-and-error-handling"> Option/Result types and error handling</a>
<a class="link subtopic" href="type-declarations.html#custom-error-types"> Custom error types</a>
<a class="link subtopic" href="type-declarations.html#generics"> Generics</a>
<a class="link" href="concurrency.html">Concurrency</a>
<a class="link subtopic" href="concurrency.html#spawning-concurrent-tasks"> Spawning Concurrent Tasks</a>
<a class="link subtopic" href="concurrency.html#channels"> Channels</a>
<a class="link subtopic" href="concurrency.html#shared-objects"> Shared Objects</a>
<a class="link" href="json.html">JSON</a>
<a class="link subtopic" href="json.html#decoding-json"> Decoding JSON</a>
<a class="link subtopic" href="json.html#encoding-json"> Encoding JSON</a>
<a class="link" href="testing.html">Testing</a>
<a class="link subtopic" href="testing.html#asserts"> Asserts</a>
<a class="link subtopic" href="testing.html#asserts-with-an-extra-message"> Asserts with an extra message</a>
<a class="link subtopic" href="testing.html#asserts-that-do-not-abort-your-program"> Asserts that do not abort your program</a>
<a class="link subtopic" href="testing.html#test-files"> Test files</a>
<a class="link subtopic" href="testing.html#running-tests"> Running tests</a>
<a class="link" href="memory-management.html">Memory management</a>
<a class="link subtopic" href="memory-management.html#control"> Control</a>
<a class="link subtopic" href="memory-management.html#stack-and-heap"> Stack and Heap</a>
<a class="link" href="orm.html">ORM</a>
<a class="link" href="writing-documentation.html">Writing Documentation</a>
<a class="link subtopic" href="writing-documentation.html#newlines-in-documentation-comments"> Newlines in Documentation Comments</a>
<a class="link" href="tools.html">Tools</a>
<a class="link subtopic" href="tools.html#v-fmt"> v fmt</a>
<a class="link subtopic" href="tools.html#v-shader"> v shader</a>
<a class="link subtopic" href="tools.html#profiling"> Profiling</a>
<a class="link" href="package-management.html">Package management</a>
<a class="link subtopic" href="package-management.html#package-commands"> Package commands</a>
<a class="link subtopic" href="package-management.html#publish-package"> Publish package</a>
<a class="link" href="attributes.html">Attributes</a>
<a class="link active" href="conditional-compilation.html">Conditional compilation</a>
<a class="link subtopic" href="conditional-compilation.html#compile-time-pseudo-variables"> Compile time pseudo variables</a>
<a class="link subtopic" href="conditional-compilation.html#compile-time-reflection"> Compile time reflection</a>
<a class="link subtopic" href="conditional-compilation.html#compile-time-code"> Compile time code</a>
<a class="link subtopic" href="conditional-compilation.html#compile-time-types"> Compile time types</a>
<a class="link subtopic" href="conditional-compilation.html#environment-specific-files"> Environment specific files</a>
<a class="link" href="debugger.html">Debugger</a>
<a class="link" href="call-stack.html">Call stack</a>
<a class="link" href="trace.html">Trace</a>
<a class="link" href="memory-unsafe-code.html">Memory-unsafe code</a>
<a class="link" href="structs-with-reference-fields.html">Structs with reference fields</a>
<a class="link" href="sizeof-and-__offsetof.html">sizeof and __offsetof</a>
<a class="link" href="limited-operator-overloading.html">Limited operator overloading</a>
<a class="link subtopic" href="limited-operator-overloading.html#implicitly-generated-overloads"> Implicitly generated overloads</a>
<a class="link subtopic" href="limited-operator-overloading.html#restriction"> Restriction</a>
<a class="link" href="performance-tuning.html">Performance tuning</a>
<a class="link subtopic" href="performance-tuning.html#tuning-operations-details"> Tuning operations details</a>
<a class="link" href="atomics.html">Atomics</a>
<a class="link" href="global-variables.html">Global Variables</a>
<a class="link" href="cross-compilation.html">Cross compilation</a>
<a class="link" href="debugging.html">Debugging</a>
<a class="link subtopic" href="debugging.html#c-backend-binaries-(default)"> C Backend binaries (Default)</a>
<a class="link subtopic" href="debugging.html#native-backend-binaries"> Native Backend binaries</a>
<a class="link subtopic" href="debugging.html#javascript-backend"> Javascript Backend</a>
<a class="link" href="v-and-c.html">V and C</a>
<a class="link subtopic" href="v-and-c.html#calling-c-from-v"> Calling C from V</a>
<a class="link subtopic" href="v-and-c.html#calling-v-from-c"> Calling V from C</a>
<a class="link subtopic" href="v-and-c.html#passing-c-compilation-flags"> Passing C compilation flags</a>
<a class="link subtopic" href="v-and-c.html#pkgconfig"> pkgconfig</a>
<a class="link subtopic" href="v-and-c.html#including-c-code"> Including C code</a>
<a class="link subtopic" href="v-and-c.html#c-types"> C types</a>
<a class="link subtopic" href="v-and-c.html#c-declarations"> C Declarations</a>
<a class="link subtopic" href="v-and-c.html#export-to-shared-library"> Export to shared library</a>
<a class="link subtopic" href="v-and-c.html#translating-c-to-v"> Translating C to V</a>
<a class="link subtopic" href="v-and-c.html#working-around-c-issues"> Working around C issues</a>
<a class="link" href="other-v-features.html">Other V Features</a>
<a class="link subtopic" href="other-v-features.html#inline-assembly"> Inline assembly</a>
<a class="link subtopic" href="other-v-features.html#hot-code-reloading"> Hot code reloading</a>
<a class="link subtopic" href="other-v-features.html#cross-platform-shell-scripts-in-v"> Cross-platform shell scripts in V</a>
<a class="link subtopic" href="other-v-features.html#vsh-scripts-with-no-extension"> Vsh scripts with no extension</a>
<a class="link" href="appendix-i-keywords.html">Appendix I: Keywords</a>
<a class="link" href="appendix-ii-operators.html">Appendix II: Operators</a>
<a class="link" href="other-online-resources.html">Other online resources</a>
<a class="link subtopic" href="other-online-resources.html#v-contributing-guide"> V contributing guide</a>
<a class="link subtopic" href="other-online-resources.html#v-language-documentation"> V language documentation</a>
<a class="link subtopic" href="other-online-resources.html#v-standard-module-documentation"> V standard module documentation</a>
<a class="link subtopic" href="other-online-resources.html#v-online-playground"> V online playground</a>
<a class="link subtopic" href="other-online-resources.html#awesome-v"> Awesome V</a>
<a class="link subtopic" href="other-online-resources.html#the-v-language-discord"> The V language Discord</a>
</div>
</section>
</div>
</nav>
</aside>
<article>
<div class="container">
<div class="content">
<div class="content-container">
<main>
<h1 class="main">Conditional compilation</h1>
<p>The goal of this feature, is to tell V to <em>not compile</em> a function, and all its calls, in the final
executable, if a provided custom flag is not passed.</p>
<p>V will still type check the function and all its calls, <em>even</em> if they will not be present in the
final executable, due to the passed -d flags.</p>
<p>In order to see it in action, run the following example with <code>v run example.v</code> once,
and then a second time with <code>v -d trace_logs example.v</code>:</p>
<div class="language-v">@[if trace_logs ?]
fn elog(s string) {
eprintln(s)
}
fn main() {
elog('some expression: ${2 + 2}') // such calls will not be done *at all*, if `-d trace_logs` is not passed
println('hi')
elog('finish')
}
</div>
<p>Conditional compilation, based on custom flags, can also be used to produce slightly different
executables, which share the majority of the same code, but where some of the logic, is needed
only some of the time, for example a network server/client program can be written like so:</p>
<div class="language-v">fn act_as_client() { ... }
fn act_as_server() { ... }
fn main() {
$if as_client ? {
act_as_client()
}
$if as_server ? {
act_as_server()
}
}
</div>
<p>To generate a <code>client.exe</code> executable do: <code>v -d as_client -o client.exe .</code>
To generate a <code>server.exe</code> executable do: <code>v -d as_server -o server.exe .</code></p>
<h2 id="compile-time-pseudo-variables">Compile time pseudo variables <a href="conditional-compilation.html#compile-time-pseudo-variables" class="header-anchor" aria-hidden="true">#</a></h2>
<p>V also gives your code access to a set of pseudo string variables,
that are substituted at compile time:</p>
<ul>
<li><code>@FN</code> => replaced with the name of the current V function.</li>
<li><code>@METHOD</code> => replaced with ReceiverType.MethodName.</li>
<li><code>@MOD</code> => replaced with the name of the current V module.</li>
<li><code>@STRUCT</code> => replaced with the name of the current V struct.</li>
<li><code>@FILE</code> => replaced with the absolute path of the V source file.</li>
<li><code>@LINE</code> => replaced with the V line number where it appears (as a string).</li>
<li><code>@FILE_LINE</code> => like <code>@FILE:@LINE</code>, but the file part is a relative path.</li>
<li><code>@LOCATION</code> => file, line and name of the current type + method; suitable for logging.</li>
<li><code>@COLUMN</code> => replaced with the column where it appears (as a string).</li>
<li><code>@VEXE</code> => replaced with the path to the V compiler.</li>
<li><code>@VEXEROOT</code> => will be substituted with the <em>folder</em>,
where the V executable is (as a string).</li>
<li><code>@VHASH</code> => replaced with the shortened commit hash of the V compiler (as a string).</li>
<li><code>@VCURRENTHASH</code> => Similar to <code>@VHASH</code>, but changes when the compiler is
recompiled on a different commit (after local modifications, or after
using git bisect etc).</li>
<li><code>@VMOD_FILE</code> => replaced with the contents of the nearest v.mod file (as a string).</li>
<li><code>@VMODHASH</code> => is replaced by the shortened commit hash, derived from the .git directory
next to the nearest v.mod file (as a string).</li>
<li><code>@VMODROOT</code> => will be substituted with the <em>folder</em>,
where the nearest v.mod file is (as a string).</li>
<li><code>@BUILD_DATE</code> => replaced with the build date, for example '2024-09-13' .</li>
<li><code>@BUILD_TIME</code> => replaced with the build time, for example '12:32:07' .</li>
<li><code>@BUILD_TIMESTAMP</code> => replaced with the build timestamp, for example '1726219885' .
Note: <code>@BUILD_DATE</code>, <code>@BUILD_TIME</code>, <code>@BUILD_TIMESTAMP</code> represent times in the UTC timezone.
By default, they are based on the current time of the compilation/build. They can be overriden
by setting the environment variable <code>SOURCE_DATE_EPOCH</code>. That is also useful while making
releases, since you can use the equivalent of this in your build system/script:
<code>export SOURCE_DATE_EPOCH=$(git log -1 --pretty=%ct) ;</code> , and then use <code>@BUILD_DATE</code> etc.,
inside your program, when you for example print your version information to users.
See also <a href="https://reproducible-builds.org/docs/source-date-epoch/">https://reproducible-builds.org/docs/source-date-epoch/</a> .</li>
</ul>
<p>The compile time pseudo variables allow you to do the following
example, which is useful while debugging/logging/tracing your code:</p>
<div class="language-v">eprintln(@LOCATION)
</div>
<p>Another example, is if you want to embed the version/name from v.mod <em>inside</em> your executable:</p>
<div class="language-v">import v.vmod
vm := vmod.decode( @VMOD_FILE ) or { panic(err) }
eprintln('${vm.name} ${vm.version}\n ${vm.description}')
</div>
<p>A program that prints its own source code (a quine):</p>
<div class="language-v">print($embed_file(@FILE).to_string())
</div>
<p>A program that prints the time when it was built:</p>
<div class="language-v">import time
println('This program, was compiled at ${time.unix(@BUILD_TIMESTAMP.i64()).format_ss_milli()} .')
</div>
<blockquote class="tip">
<p>[!NOTE]
you can have arbitrary source code in the file, without problems, since the full file
will be embedded into the executable, produced by compiling it. Also note that printing
is done with <code>print</code> and not <code>println</code>, to not add another new line, missing in the
source code.</p>
</blockquote>
<h2 id="compile-time-reflection">Compile time reflection <a href="conditional-compilation.html#compile-time-reflection" class="header-anchor" aria-hidden="true">#</a></h2>
<p><code>$</code> is used as a prefix for compile time (also referred to as 'comptime') operations.</p>
<p>Having built-in JSON support is nice, but V also allows you to create efficient
serializers for any data format. V has compile time <code>if</code> and <code>for</code> constructs:</p>
<h3 id=".fields"><h4 id="comptime-fields">.fields</h4> <a href="#.fields" class="header-anchor" aria-hidden="true">#</a></h3>
<p>You can iterate over struct fields using <code>.fields</code>, it also works with generic types
(e.g. <code>T.fields</code>) and generic arguments (e.g. <code>param.fields</code> where <code>fn gen[T](param T) {</code>).</p>
<div class="language-v">struct User {
name string
age int
}
fn main() {
$for field in User.fields {
$if field.typ is string {
println('${field.name} is of type string')
}
}
}
// Output:
// name is of type string
</div>
<h3 id=".values"><h4 id="comptime-values">.values</h4> <a href="#.values" class="header-anchor" aria-hidden="true">#</a></h3>
<p>You can read <a href="type-declarations.html#enums">Enum</a> values and their attributes.</p>
<div class="language-v">enum Color {
red @[RED] // first attribute
blue @[BLUE] // second attribute
}
fn main() {
$for e in Color.values {
println(e.name)
println(e.attrs)
}
}
// Output:
// red
// ['RED']
// blue
// ['BLUE']
</div>
<h3 id=".attributes"><h4 id="comptime-attrs">.attributes</h4> <a href="#.attributes" class="header-anchor" aria-hidden="true">#</a></h3>
<p>You can read <a href="structs.html">Struct</a> attributes.</p>
<div class="language-v">@[COLOR]
struct Foo {
a int
}
fn main() {
$for e in Foo.attributes {
println(e)
}
}
// Output:
// StructAttribute{
// name: 'COLOR'
// has_arg: false
// arg: ''
// kind: plain
// }
</div>
<h3 id=".variants"><h4 id="comptime-variants">.variants</h4> <a href="#.variants" class="header-anchor" aria-hidden="true">#</a></h3>
<p>You can read variant types from <a href="type-declarations.html#sum-types">Sum type</a>.</p>
<div class="language-v">type MySum = int | string
fn main() {
$for v in MySum.variants {
$if v.typ is int {
println('has int type')
} $else $if v.typ is string {
println('has string type')
}
}
}
// Output:
// has int type
// has string type
</div>
<h3 id=".methods"><h4 id="comptime-methods">.methods</h4> <a href="#.methods" class="header-anchor" aria-hidden="true">#</a></h3>
<p>You can retrieve information about struct methods.</p>
<div class="language-v">struct Foo {
}
fn (f Foo) test() int {
return 123
}
fn (f Foo) test2() string {
return 'foo'
}
fn main() {
foo := Foo{}
$for m in Foo.methods {
$if m.return_type is int {
print('${m.name} returns int: ')
println(foo.$method())
} $else $if m.return_type is string {
print('${m.name} returns string: ')
println(foo.$method())
}
}
}
// Output:
// test returns int: 123
// test2 returns string: foo
</div>
<h3 id=".params"><h4 id="comptime-method-params">.params</h4> <a href="#.params" class="header-anchor" aria-hidden="true">#</a></h3>
<p>You can retrieve information about struct method params.</p>
<div class="language-v">struct Test {
}
fn (t Test) foo(arg1 int, arg2 string) {
}
fn main() {
$for m in Test.methods {
$for param in m.params {
println('${typeof(param.typ).name}: ${param.name}')
}
}
}
// Output:
// int: arg1
// string: arg2
</div>
<p>See <a href="https://github.com/vlang/v/blob/master/examples/compiletime/reflection.v"><code>examples/compiletime/reflection.v</code></a>
for a more complete example.</p>
<h2 id="compile-time-code">Compile time code <a href="conditional-compilation.html#compile-time-code" class="header-anchor" aria-hidden="true">#</a></h2>
<h3 id="$if-condition"><code>$if</code> condition <a href="conditional-compilation.html#$if-condition" class="header-anchor" aria-hidden="true">#</a></h3>
<div class="language-v">fn main() {
// Support for multiple conditions in one branch
$if ios || android {
println('Running on a mobile device!')
}
$if linux && x64 {
println('64-bit Linux.')
}
// Usage as expression
os := $if windows { 'Windows' } $else { 'UNIX' }
println('Using ${os}')
// $else-$if branches
$if tinyc {
println('tinyc')
} $else $if clang {
println('clang')
} $else $if gcc {
println('gcc')
} $else {
println('different compiler')
}
$if test {
println('testing')
}
// v -cg ...
$if debug {
println('debugging')
}
// v -prod ...
$if prod {
println('production build')
}
// v -d option ...
$if option ? {
println('custom option')
}
}
</div>
<p>If you want an <code>if</code> to be evaluated at compile time it must be prefixed with a <code>$</code> sign.
Right now it can be used to detect an OS, compiler, platform or compilation options.
<code>$if debug</code> is a special option like <code>$if windows</code> or <code>$if x32</code>, it's enabled if the program
is compiled with <code>v -g</code> or <code>v -cg</code>.
If you're using a custom ifdef, then you do need <code>$if option ? {}</code> and compile with<code>v -d option</code>.
Full list of builtin options:</p>
<table>
<thead>
<tr>
<th>OS</th>
<th>Compilers</th>
<th>Platforms</th>
<th>Other</th>
</tr>
</thead>
<tbody>
<tr>
<td><code>windows</code>, <code>linux</code>, <code>macos</code></td>
<td><code>gcc</code>, <code>tinyc</code></td>
<td><code>amd64</code>, <code>arm64</code>, <code>aarch64</code></td>
<td><code>debug</code>, <code>prod</code>, <code>test</code></td>
</tr>
<tr>
<td><code>darwin</code>, <code>ios</code>, <code>bsd</code></td>
<td><code>clang</code>, <code>mingw</code></td>
<td><code>i386</code>, <code>arm32</code></td>
<td><code>js</code>, <code>glibc</code>, <code>prealloc</code></td>
</tr>
<tr>
<td><code>freebsd</code>, <code>openbsd</code>, <code>netbsd</code></td>
<td><code>msvc</code></td>
<td><code>rv64</code>, <code>rv32</code></td>
<td><code>no_bounds_checking</code>, <code>freestanding</code></td>
</tr>
<tr>
<td><code>android</code>, <code>mach</code>, <code>dragonfly</code></td>
<td><code>cplusplus</code></td>
<td><code>x64</code>, <code>x32</code></td>
<td><code>no_segfault_handler</code>, <code>no_backtrace</code></td>
</tr>
<tr>
<td><code>gnu</code>, <code>hpux</code>, <code>haiku</code>, <code>qnx</code></td>
<td></td>
<td><code>little_endian</code>, <code>big_endian</code></td>
<td><code>no_main</code>, <code>fast_math</code>, <code>apk</code>, <code>threads</code></td>
</tr>
<tr>
<td><code>solaris</code>, <code>termux</code></td>
<td></td>
<td></td>
<td><code>js_node</code>, <code>js_browser</code>, <code>js_freestanding</code></td>
</tr>
<tr>
<td><code>serenity</code>, <code>vinix</code>, <code>plan9</code></td>
<td></td>
<td></td>
<td><code>interpreter</code>, <code>es5</code>, <code>profile</code>, <code>wasm32</code></td>
</tr>
<tr>
<td></td>
<td></td>
<td></td>
<td><code>wasm32_emscripten</code>, <code>wasm32_wasi</code></td>
</tr>
<tr>
<td></td>
<td></td>
<td></td>
<td><code>native</code>, <code>autofree</code></td>
</tr>
</tbody>
</table>
<h3 id="$embed_file"><code>$embed_file</code> <a href="conditional-compilation.html#$embed_file" class="header-anchor" aria-hidden="true">#</a></h3>
<div class="language-v">import os
fn main() {
embedded_file := $embed_file('v.png')
os.write_file('exported.png', embedded_file.to_string())!
}
</div>
<p>V can embed arbitrary files into the executable with the <code>$embed_file(<path>)</code>
compile time call. Paths can be absolute or relative to the source file.</p>
<p>Note that by default, using <code>$embed_file(file)</code>, will always embed the whole content
of the file, but you can modify that behaviour by passing: <code>-d embed_only_metadata</code>
when compiling your program. In that case, the file will not be embedded. Instead,
it will be loaded <em>the first time</em> your program calls <code>embedded_file.data()</code> at runtime,
making it easier to change in external editor programs, without needing to recompile
your program.</p>
<p>Embedding a file inside your executable, will increase its size, but
it will make it more self contained and thus easier to distribute.
When that happens (the default), <code>embedded_file.data()</code> will cause <em>no IO</em>,
and it will always return the same data.</p>
<p><code>$embed_file</code> supports compression of the embedded file when compiling with <code>-prod</code>.
Currently only one compression type is supported: <code>zlib</code>.</p>
<div class="language-v">import os
fn main() {
embedded_file := $embed_file('x.css', .zlib) // compressed using zlib
os.write_file('exported.css', embedded_file.to_string())!
}
</div>
<p>Note: compressing binary assets like png or zip files, usually will not gain you much,
and in some cases may even take more space in the final executable, since they are
already compressed.</p>
<p><code>$embed_file</code> returns
<a href="https://modules.vlang.io/v.embed_file.html#EmbedFileData">EmbedFileData</a>
which could be used to obtain the file contents as <code>string</code> or <code>[]u8</code>.</p>
<h3 id="$tmpl-for-embedding-and-parsing-v-template-files"><code>$tmpl</code> for embedding and parsing V template files <a href="conditional-compilation.html#$tmpl-for-embedding-and-parsing-v-template-files" class="header-anchor" aria-hidden="true">#</a></h3>
<p>V has a simple template language for text and html templates, and they can easily
be embedded via <code>$tmpl('path/to/template.txt')</code>:</p>
<div class="language-v">fn build() string {
name := 'Peter'
age := 25
numbers := [1, 2, 3]
return $tmpl('1.txt')
}
fn main() {
println(build())
}
</div>
<p>1.txt:</p>
<pre><code>name: @name
age: @age
numbers: @numbers
@for number in numbers
@number
@end
</code></pre>
<p>output:</p>
<pre><code>name: Peter
age: 25
numbers: [1, 2, 3]
1
2
3
</code></pre>
<p>See more <a href="https://github.com/vlang/v/blob/master/vlib/v/TEMPLATES.md">details</a></p>
<h3 id="$env"><code>$env</code> <a href="conditional-compilation.html#$env" class="header-anchor" aria-hidden="true">#</a></h3>
<div class="language-v">module main
fn main() {
compile_time_env := $env('ENV_VAR')
println(compile_time_env)
}
</div>
<p>V can bring in values at compile time from environment variables.
<code>$env('ENV_VAR')</code> can also be used in top-level <code>#flag</code> and <code>#include</code> statements:
<code>#flag linux -I $env('JAVA_HOME')/include</code>.</p>
<h3 id="$d"><code>$d</code> <a href="conditional-compilation.html#$d" class="header-anchor" aria-hidden="true">#</a></h3>
<p>V can bring in values at compile time from <code>-d ident=value</code> flag defines, passed on
the command line to the compiler. You can also pass <code>-d ident</code>, which will have the
same meaning as passing <code>-d ident=true</code>.</p>
<p>To get the value in your code, use: <code>$d('ident', default)</code>, where <code>default</code>
can be <code>false</code> for booleans, <code>0</code> or <code>123</code> for i64 numbers, <code>0.0</code> or <code>113.0</code>
for f64 numbers, <code>'a string'</code> for strings.</p>
<p>When a flag is not provided via the command line, <code>$d()</code> will return the <code>default</code>
value provided as the <em>second</em> argument.</p>
<div class="language-v">module main
const my_i64 = $d('my_i64', 1024)
fn main() {
compile_time_value := $d('my_string', 'V')
println(compile_time_value)
println(my_i64)
}
</div>
<p>Running the above with <code>v run .</code> will output:</p>
<pre><code>V
1024
</code></pre>
<p>Running the above with <code>v -d my_i64=4096 -d my_string="V rocks" run .</code> will output:</p>
<pre><code>V rocks
4096
</code></pre>
<p>Here is an example of how to use the default values, which have to be <em>pure</em> literals:</p>
<div class="language-v">fn main() {
val_str := $d('id_str', 'value') // can be changed by providing `-d id_str="my id"`
val_f64 := $d('id_f64', 42.0) // can be changed by providing `-d id_f64=84.0`
val_i64 := $d('id_i64', 56) // can be changed by providing `-d id_i64=123`
val_bool := $d('id_bool', false) // can be changed by providing `-d id_bool=true`
val_char := $d('id_char', `f`) // can be changed by providing `-d id_char=v`
println(val_str)
println(val_f64)
println(val_i64)
println(val_bool)
println(rune(val_char))
}
</div>
<p><code>$d('ident','value')</code> can also be used in top-level statements like <code>#flag</code> and <code>#include</code>:
<code>#flag linux -I $d('my_include','/usr')/include</code>. The default value for <code>$d</code> when used in these
statements should be literal <code>string</code>s.</p>
<p><code>$d('ident', false)</code> can also be used inside <code>$if $d('ident', false) {</code> statements,
granting you the ability to selectively turn on/off certain sections of code, at compile
time, without modifying your source code, or keeping different versions of it.</p>
<h3 id="$compile_error-and-$compile_warn"><code>$compile_error</code> and <code>$compile_warn</code> <a href="conditional-compilation.html#$compile_error-and-$compile_warn" class="header-anchor" aria-hidden="true">#</a></h3>
<p>These two comptime functions are very useful for displaying custom errors/warnings during
compile time.</p>
<p>Both receive as their only argument a string literal that contains the message to display:</p>
<div class="language-v">// x.v
module main
$if linux {
$compile_error('Linux is not supported')
}
fn main() {
}
$ v run x.v
x.v:4:5: error: Linux is not supported
2 |
3 | $if linux {
4 | $compile_error('Linux is not supported')
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
5 | }
6 |
</div>
<h2 id="compile-time-types">Compile time types <a href="conditional-compilation.html#compile-time-types" class="header-anchor" aria-hidden="true">#</a></h2>
<p>Compile time types group multiple types into a general higher-level type. This is useful in
functions with generic parameters, where the input type must have a specific property, for example
the <code>.len</code> attribute in arrays.</p>
<p>V supports the following compile time types:</p>
<ul>
<li><code>$alias</code> => matches <a href="type-declarations.html#type-aliases">Type aliases</a>.</li>
<li><code>$array</code> => matches <a href="v-types.html#arrays">Arrays</a> and <a href="v-types.html#fixed-size-arrays">Fixed Size Arrays</a>.</li>
<li><code>$array_dynamic</code> => matches <a href="v-types.html#arrays">Arrays</a>, but not <a href="v-types.html#fixed-size-arrays">Fixed Size Arrays</a>.</li>
<li><code>$array_fixed</code> => matches <a href="v-types.html#fixed-size-arrays">Fixed Size Arrays</a>, but not <a href="v-types.html#arrays">Arrays</a></li>
<li><code>$enum</code> => matches <a href="type-declarations.html#enums">Enums</a>.</li>
<li><code>$float</code> => matches <code>f32</code>, <code>f64</code> and float literals.</li>
<li><code>$function</code> => matches <a href="type-declarations.html#function-types">Function Types</a>.</li>
<li><code>$int</code> => matches <code>int</code>, <code>i8</code>, <code>i16</code>, <code>i32</code>, <code>i64</code>, <code>u8</code>, <code>u16</code>, <code>u32</code>, <code>u64</code>, <code>isize</code>, <code>usize</code>
and integer literals.</li>
<li><code>$interface</code> => matches <a href="type-declarations.html#interfaces">Interfaces</a>.</li>
<li><code>$map</code> => matches <a href="v-types.html#maps">Maps</a>.</li>
<li><code>$option</code> => matches <a href="type-declarations.html#optionresult-types-and-error-handling">Option Types</a>.</li>
<li><code>$struct</code> => matches <a href="structs.html">Structs</a>.</li>
<li><code>$sumtype</code> => matches <a href="type-declarations.html#sum-types">Sum Types</a>.</li>
<li><code>$string</code> => matches <a href="v-types.html#strings">Strings</a>.</li>
</ul>
<h2 id="environment-specific-files">Environment specific files <a href="conditional-compilation.html#environment-specific-files" class="header-anchor" aria-hidden="true">#</a></h2>
<p>If a file has an environment-specific suffix, it will only be compiled for that environment.</p>
<ul>
<li><code>.js.v</code> => will be used only by the JS backend. These files can contain JS. code.</li>
<li><code>.c.v</code> => will be used only by the C backend. These files can contain C. code.</li>
<li><code>.native.v</code> => will be used only by V's native backend.</li>
<li><code>_nix.c.v</code> => will be used only on Unix systems (non Windows).</li>
<li><code>_${os}.c.v</code> => will be used only on the specific <code>os</code> system.
For example, <code>_windows.c.v</code> will be used only when compiling on Windows, or with <code>-os windows</code>.</li>
<li><code>_default.c.v</code> => will be used only if there is NOT a more specific platform file.
For example, if you have both <code>file_linux.c.v</code> and <code>file_default.c.v</code>,
and you are compiling for linux, then only <code>file_linux.c.v</code> will be used,
and <code>file_default.c.v</code> will be ignored.</li>
</ul>
<p>Here is a more complete example:</p>
<p><code>main.v</code>:</p>
<div class="language-v">module main
fn main() { println(message) }
</div>
<p><code>main_default.c.v</code>:</p>
<div class="language-v">module main
const message = 'Hello world'
</div>
<p><code>main_linux.c.v</code>:</p>
<div class="language-v">module main
const message = 'Hello linux'
</div>
<p><code>main_windows.c.v</code>:</p>
<div class="language-v">module main
const message = 'Hello windows'
</div>
<p>With the example above:</p>
<ul>
<li><p>when you compile for Windows, you will get <code>Hello windows</code></p>
</li>
<li><p>when you compile for Linux, you will get <code>Hello linux</code></p>
</li>
<li><p>when you compile for any other platform, you will get the
non specific <code>Hello world</code> message.</p>
</li>
<li><p><code>_d_customflag.v</code> => will be used <em>only</em> if you pass <code>-d customflag</code> to V.
That corresponds to <code>$if customflag ? {}</code>, but for a whole file, not just a
single block. <code>customflag</code> should be a snake_case identifier, it can not
contain arbitrary characters (only lower case latin letters + numbers + <code>_</code>).</p>
<blockquote class="tip">
<p><strong>Note</strong></p>
<p>A combinatorial <code>_d_customflag_linux.c.v</code> postfix will not work.
If you do need a custom flag file, that has platform dependent code, use the
postfix <code>_d_customflag.v</code>, and then use platform dependent compile time
conditional blocks inside it, i.e. <code>$if linux {}</code> etc.</p>
</blockquote>
</li>
<li><p><code>_notd_customflag.v</code> => similar to _d_customflag.v, but will be used
<em>only</em> if you do NOT pass <code>-d customflag</code> to V.</p>
</li>
</ul>
<p>See also <a href="cross-compilation.html">Cross Compilation</a>.</p>
</main>
<footer>
<div class="edit-info">
<div class="edit-link">
<a class="link edit-link-button"
href="https://github.com/vlang/v/edit/master/doc/docs.md#L5923"
target="_blank" rel="noreferrer">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"
class="edit-link-icon">
<path
d="M18,23H4c-1.7,0-3-1.3-3-3V6c0-1.7,1.3-3,3-3h7c0.6,0,1,0.4,1,1s-0.4,1-1,1H4C3.4,5,3,5.4,3,6v14c0,0.6,0.4,1,1,1h14c0.6,0,1-0.4,1-1v-7c0-0.6,0.4-1,1-1s1,0.4,1,1v7C21,21.7,19.7,23,18,23z">
</path>
<path
d="M8,17c-0.3,0-0.5-0.1-0.7-0.3C7,16.5,6.9,16.1,7,15.8l1-4c0-0.2,0.1-0.3,0.3-0.5l9.5-9.5c1.2-1.2,3.2-1.2,4.4,0c1.2,1.2,1.2,3.2,0,4.4l-9.5,9.5c-0.1,0.1-0.3,0.2-0.5,0.3l-4,1C8.2,17,8.1,17,8,17zM9.9,12.5l-0.5,2.1l2.1-0.5l9.3-9.3c0.4-0.4,0.4-1.1,0-1.6c-0.4-0.4-1.2-0.4-1.6,0l0,0L9.9,12.5z M18.5,2.5L18.5,2.5L18.5,2.5z">
</path>
</svg>
Edit this page on GitHub
</a>
</div>
<div class="last-updated">
<time datetime="2024-11-27T19:07:56.708Z">Last updated:
2024-11-27 21:07:56, V Commit: <a
href="https://github.com/vlang/v/commit/066384ce8c13bcac052d60f1983f40674ba9b698">066384c</a></time>
</div>
</div>
<!-- only-prev or only-next -->
<div class="prev-next">
<div class="page prev-page">
<a class="page-link" href="attributes.html">
<span class="desc">Previous page</span>
<span class="title">Attributes</span>
</a>
</div>
<div class="page next-page">
<a class="page-link" href="debugger.html">
<span class="desc">Next page</span>
<span class="title">Debugger</span>
</a>
</div>
</div>
</footer>
</div>
</div>
<div class="aside">
<div class="aside-content">
<div class="aside-container">
<div class="title">On this page</div>
<nav>
<ul>
<li class="active">
<a href="#compile-time-pseudo-variables">Compile time pseudo variables</a>
</li>
<li class="active">
<a href="#compile-time-reflection">Compile time reflection</a>
</li>
<li class="active">
<a href="#compile-time-code">Compile time code</a>
</li>
<li class="active">
<a href="#compile-time-types">Compile time types</a>
</li>
<li class="active">
<a href="#environment-specific-files">Environment specific files</a>
</li>
</ul>
</nav>
</div>
<div class="aside-footer"></div>
</div>
</div>
</div>
</article>
</div>
<script>
document.addEventListener('DOMContentLoaded', () => {
const currentNavigationItem = document.querySelector(".link.active");
if (currentNavigationItem !== null) {
currentNavigationItem.scrollIntoView({ block: "center", behavior: "auto" });
}
})
</script>
<script src="assets/scripts/vlang-playground.js"></script>
<script>
document.addEventListener('DOMContentLoaded', () => {
const playgroundElements = [...document.querySelectorAll(".language-v"), ...document.querySelectorAll(".language-c")];
playgroundElements.forEach(playgroundElement => {
const playground = Playground.create(playgroundElement, {
fontSize: "14px",
highlightOnly: true,
showFooter: false,
showLineNumbers: false,
});
playgrounds.push(playground);
});
})
</script>
<script src="assets/scripts/theme.js"></script>
<script>
document.addEventListener("keydown", (event) => {
if (event.key === "ArrowLeft") {
window.open("attributes.html", "_self")
}
if (event.key === "ArrowRight") {
window.open("debugger.html", "_self")
}
})
//window.onload = () => {
//const html = document.querySelector("html");
//html.style.opacity = '1'
//}
</script>
<script>
document.getElementsByClassName("aside-topics-open")[0].addEventListener("click", (event) => {
document.getElementsByClassName("aside-topics")[0].style.setProperty('display', 'block')
})
document.getElementsByClassName("aside-topics-close")[0].addEventListener("click", (event) => {
document.getElementsByClassName("aside-topics")[0].style.setProperty('display', 'none')
})
</script>
<script src="assets/scripts/titles_to_fnames.js"></script>
<script src="assets/scripts/search.js"></script>
</body>
</html>