-
Notifications
You must be signed in to change notification settings - Fork 4
/
v-types.html
1382 lines (1224 loc) · 70.6 KB
/
v-types.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 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>
<div class="items">
<a class="link" href="installing-v-from-source.html">Installing V from source</a>
<div class="items">
<a class="link" href="upgrading-v-to-latest-version.html">Upgrading V to latest version</a>
<div class="items">
<a class="link" href="packaging-v-for-distribution.html">Packaging V for distribution</a>
<div class="items">
<a class="link" href="getting-started.html">Getting started</a>
<div class="items">
<a class="link" href="hello-world.html">Hello World</a>
<div class="items">
<a class="link" href="running-a-project-folder-with-several-files.html">Running a project folder with several files</a>
<div class="items">
<a class="link" href="comments.html">Comments</a>
<div class="items">
<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>
<div class="items">
<a class="link" href="symbol-visibility.html">Symbol visibility</a>
<div class="items">
<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>
<div class="items">
<a class="link active" 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>
<div class="items">
<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-import-aliasing"> Module import aliasing</a>
<div class="items">
<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>
<div class="items">
<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>
<div class="items">
<a class="link" href="unions.html">Unions</a>
<div class="items">
<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>
<div class="items">
<a class="link" href="references.html">References</a>
<div class="items">
<a class="link" href="constants.html">Constants</a>
<a class="link subtopic" href="constants.html#required-module-prefix"> Required module prefix</a>
<div class="items">
<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>
<div class="items">
<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>
<div class="items">
<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>
<div class="items">
<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>
<div class="items">
<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>
<div class="items">
<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>
<div class="items">
<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>
<div class="items">
<a class="link" href="orm.html">ORM</a>
<div class="items">
<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>
<div class="items">
<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>
<div class="items">
<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>
<div class="items">
<a class="link" href="attributes.html">Attributes</a>
<div class="items">
<a class="link" 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>
<div class="items">
<a class="link" href="debugger.html">Debugger</a>
<div class="items">
<a class="link" href="call-stack.html">Call stack</a>
<div class="items">
<a class="link" href="trace.html">Trace</a>
<div class="items">
<a class="link" href="memory-unsafe-code.html">Memory-unsafe code</a>
<div class="items">
<a class="link" href="structs-with-reference-fields.html">Structs with reference fields</a>
<div class="items">
<a class="link" href="sizeof-and-__offsetof.html">sizeof and __offsetof</a>
<div class="items">
<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>
<div class="items">
<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>
<div class="items">
<a class="link" href="atomics.html">Atomics</a>
<div class="items">
<a class="link" href="global-variables.html">Global Variables</a>
<div class="items">
<a class="link" href="cross-compilation.html">Cross compilation</a>
<div class="items">
<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>
<div class="items">
<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>
<div class="items">
<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>
<div class="items">
<a class="link" href="appendix-i-keywords.html">Appendix I: Keywords</a>
<div class="items">
<a class="link" href="appendix-ii-operators.html">Appendix II: Operators</a>
</div>
</section>
</div>
</nav>
</aside>
<article>
<div class="container">
<div class="content">
<div class="content-container">
<main>
<h1 class="main">V Types</h1>
<h2 id="primitive-types">Primitive types <a href="v-types.html#primitive-types" class="header-anchor" aria-hidden="true">#</a></h2>
<div class="language-v">bool
string
i8 i16 int i64 i128 (soon)
u8 u16 u32 u64 u128 (soon)
rune // represents a Unicode code point
f32 f64
isize, usize // platform-dependent, the size is how many bytes it takes to reference any location in memory
voidptr // this one is mostly used for [C interoperability](#v-and-c)
any // similar to C's void* and Go's interface{}
</div>
<blockquote class="tip">
<p>[!NOTE]
Unlike C and Go, <code>int</code> is always a 32 bit integer.</p>
</blockquote>
<p>There is an exception to the rule that all operators
in V must have values of the same type on both sides. A small primitive type
on one side can be automatically promoted if it fits
completely into the data range of the type on the other side.
These are the allowed possibilities:</p>
<div class="language-v"> i8 → i16 → int → i64
↘ ↘
f32 → f64
↗ ↗
u8 → u16 → u32 → u64 ⬎
↘ ↘ ↘ ptr
i8 → i16 → int → i64 ⬏
</div>
<p>An <code>int</code> value for example can be automatically promoted to <code>f64</code>
or <code>i64</code> but not to <code>u32</code>. (<code>u32</code> would mean loss of the sign for
negative values).
Promotion from <code>int</code> to <code>f32</code>, however, is currently done automatically
(but can lead to precision loss for large values).</p>
<p>Literals like <code>123</code> or <code>4.56</code> are treated in a special way. They do
not lead to type promotions, however they default to <code>int</code> and <code>f64</code>
respectively, when their type has to be decided:</p>
<div class="language-v">u := u16(12)
v := 13 + u // v is of type `u16` - no promotion
x := f32(45.6)
y := x + 3.14 // y is of type `f32` - no promotion
a := 75 // a is of type `int` - default for int literal
b := 14.7 // b is of type `f64` - default for float literal
c := u + a // c is of type `int` - automatic promotion of `u`'s value
d := b + x // d is of type `f64` - automatic promotion of `x`'s value
</div>
<h2 id="strings">Strings <a href="v-types.html#strings" class="header-anchor" aria-hidden="true">#</a></h2>
<div class="language-v">name := 'Bob'
assert name.len == 3 // will print 3
assert name[0] == u8(66) // indexing gives a byte, u8(66) == `B`
assert name[1..3] == 'ob' // slicing gives a string 'ob'
// escape codes
windows_newline := '\r\n' // escape special characters like in C
assert windows_newline.len == 2
// arbitrary bytes can be directly specified using `\x##` notation where `#` is
// a hex digit
aardvark_str := '\x61ardvark'
assert aardvark_str == 'aardvark'
assert '\xc0'[0] == u8(0xc0)
// or using octal escape `\###` notation where `#` is an octal digit
aardvark_str2 := '\141ardvark'
assert aardvark_str2 == 'aardvark'
// Unicode can be specified directly as `\u####` where # is a hex digit
// and will be converted internally to its UTF-8 representation
star_str := '\u2605' // ★
assert star_str == '★'
assert star_str == '\xe2\x98\x85' // UTF-8 can be specified this way too.
</div>
<p>In V, a string is a read-only array of bytes. All Unicode characters are encoded using UTF-8:</p>
<div class="language-v">s := 'hello 🌎' // emoji takes 4 bytes
assert s.len == 10
arr := s.bytes() // convert `string` to `[]u8`
assert arr.len == 10
s2 := arr.bytestr() // convert `[]u8` to `string`
assert s2 == s
</div>
<p>String values are immutable. You cannot mutate elements:</p>
<div class="language-v">mut s := 'hello 🌎'
s[0] = `H` // not allowed
</div>
<blockquote class="tip">
<p>error: cannot assign to <code>s[i]</code> since V strings are immutable</p>
</blockquote>
<p>Note that indexing a string will produce a <code>u8</code> (byte), not a <code>rune</code> nor another <code>string</code>. Indexes
correspond to <em>bytes</em> in the string, not Unicode code points. If you want to convert the <code>u8</code> to a
<code>string</code>, use the <code>.ascii_str()</code> method on the <code>u8</code>:</p>
<div class="language-v">country := 'Netherlands'
println(country[0]) // Output: 78
println(country[0].ascii_str()) // Output: N
</div>
<p>Both single and double quotes can be used to denote strings. For consistency, <code>vfmt</code> converts double
quotes to single quotes unless the string contains a single quote character.</p>
<p>For raw strings, prepend <code>r</code>. Escape handling is not done for raw strings:</p>
<div class="language-v">s := r'hello\nworld' // the `\n` will be preserved as two characters
println(s) // "hello\nworld"
</div>
<p>Strings can be easily converted to integers:</p>
<div class="language-v">s := '42'
n := s.int() // 42
// all int literals are supported
assert '0xc3'.int() == 195
assert '0o10'.int() == 8
assert '0b1111_0000_1010'.int() == 3850
assert '-0b1111_0000_1010'.int() == -3850
</div>
<p>For more advanced <code>string</code> processing and conversions, refer to the
<a href="https://modules.vlang.io/strconv.html">vlib/strconv</a> module.</p>
<h3 id="string-interpolation">String interpolation <a href="v-types.html#string-interpolation" class="header-anchor" aria-hidden="true">#</a></h3>
<p>Basic interpolation syntax is pretty simple - use <code>${</code> before a variable name and <code>}</code> after. The
variable will be converted to a string and embedded into the literal:</p>
<div class="language-v">name := 'Bob'
println('Hello, ${name}!') // Hello, Bob!
</div>
<p>It also works with fields: <code>'age = ${user.age}'</code>. You may also use more complex expressions:
<code>'can register = ${user.age > 13}'</code>.</p>
<p>Format specifiers similar to those in C's <code>printf()</code> are also supported. <code>f</code>, <code>g</code>, <code>x</code>, <code>o</code>, <code>b</code>,
etc. are optional and specify the output format. The compiler takes care of the storage size, so
there is no <code>hd</code> or <code>llu</code>.</p>
<p>To use a format specifier, follow this pattern:</p>
<p><code>${varname:[flags][width][.precision][type]}</code></p>
<ul>
<li><p>flags: may be zero or more of the following: <code>-</code> to left-align output within the field, <code>0</code> to use
<code>0</code> as the padding character instead of the default <code>space</code> character.</p>
<blockquote class="tip">
<p><strong>Note</strong></p>
<p>V does not currently support the use of <code>'</code> or <code>#</code> as format flags, and V supports but
doesn't need <code>+</code> to right-align since that's the default.</p>
</blockquote>
</li>
<li><p>width: may be an integer value describing the minimum width of total field to output.</p>
</li>
<li><p>precision: an integer value preceded by a <code>.</code> will guarantee that many digits after the decimal
point without any insignificant trailing zeros. If displaying insignificant zero's is desired,
append a <code>f</code> specifier to the precision value (see examples below). Applies only to float
variables and is ignored for integer variables.</p>
</li>
<li><p>type: <code>f</code> and <code>F</code> specify the input is a float and should be rendered as such, <code>e</code> and <code>E</code> specify
the input is a float and should be rendered as an exponent (partially broken), <code>g</code> and <code>G</code> specify
the input is a float--the renderer will use floating point notation for small values and exponent
notation for large values, <code>d</code> specifies the input is an integer and should be rendered in base-10
digits, <code>x</code> and <code>X</code> require an integer and will render it as hexadecimal digits, <code>o</code> requires an
integer and will render it as octal digits, <code>b</code> requires an integer and will render it as binary
digits, <code>s</code> requires a string (almost never used).</p>
<blockquote class="tip">
<p><strong>Note</strong></p>
<p>When a numeric type can render alphabetic characters, such as hex strings or special values
like <code>infinity</code>, the lowercase version of the type forces lowercase alphabetics and the
uppercase version forces uppercase alphabetics.</p>
</blockquote>
<blockquote class="tip">
<p><strong>Note</strong></p>
<p>In most cases, it's best to leave the format type empty. Floats will be rendered by
default as <code>g</code>, integers will be rendered by default as <code>d</code>, and <code>s</code> is almost always redundant.
There are only three cases where specifying a type is recommended:</p>
</blockquote>
</li>
<li><p>format strings are parsed at compile time, so specifying a type can help detect errors then</p>
</li>
<li><p>format strings default to using lowercase letters for hex digits and the <code>e</code> in exponents. Use a
uppercase type to force the use of uppercase hex digits and an uppercase <code>E</code> in exponents.</p>
</li>
<li><p>format strings are the most convenient way to get hex, binary or octal strings from an integer.</p>
</li>
</ul>
<p>See
<a href="https://en.wikipedia.org/wiki/Printf_format_string#Format_placeholder_specification">Format Placeholder Specification</a>
for more information.</p>
<div class="language-v">x := 123.4567
println('[${x:.2}]') // round to two decimal places => [123.46]
println('[${x:10}]') // right-align with spaces on the left => [ 123.457]
println('[${int(x):-10}]') // left-align with spaces on the right => [123 ]
println('[${int(x):010}]') // pad with zeros on the left => [0000000123]
println('[${int(x):b}]') // output as binary => [1111011]
println('[${int(x):o}]') // output as octal => [173]
println('[${int(x):X}]') // output as uppercase hex => [7B]
println('[${10.0000:.2}]') // remove insignificant 0s at the end => [10]
println('[${10.0000:.2f}]') // do show the 0s at the end, even though they do not change the number => [10.00]
</div>
<p>V also has <code>r</code> and <code>R</code> switches, which will repeat the string the specified amount of times.</p>
<div class="language-v">println('[${'abc':3r}]') // [abcabcabc]
println('[${'abc':3R}]') // [ABCABCABC]
</div>
<h3 id="string-operators">String operators <a href="v-types.html#string-operators" class="header-anchor" aria-hidden="true">#</a></h3>
<div class="language-v">name := 'Bob'
bobby := name + 'by' // + is used to concatenate strings
println(bobby) // "Bobby"
mut s := 'hello '
s += 'world' // `+=` is used to append to a string
println(s) // "hello world"
</div>
<p>All operators in V must have values of the same type on both sides. You cannot concatenate an
integer to a string:</p>
<div class="language-v">age := 10
println('age = ' + age) // not allowed
</div>
<blockquote class="tip">
<p>error: infix expr: cannot use <code>int</code> (right expression) as <code>string</code></p>
</blockquote>
<p>We have to either convert <code>age</code> to a <code>string</code>:</p>
<div class="language-v">age := 11
println('age = ' + age.str())
</div>
<p>or use string interpolation (preferred):</p>
<div class="language-v">age := 12
println('age = ${age}')
</div>
<p>See all methods of <a href="https://modules.vlang.io/index.html#string">string</a>
and related modules <a href="https://modules.vlang.io/strings.html">strings</a>,
<a href="https://modules.vlang.io/strconv.html">strconv</a>.</p>
<h2 id="runes">Runes <a href="v-types.html#runes" class="header-anchor" aria-hidden="true">#</a></h2>
<p>A <code>rune</code> represents a single Unicode character and is an alias for <code>u32</code>.
To denote them, use <code>`</code> (backticks) :</p>
<div class="language-v">rocket := `🚀`
</div>
<p>A <code>rune</code> can be converted to a UTF-8 string by using the <code>.str()</code> method.</p>
<div class="language-v">rocket := `🚀`
assert rocket.str() == '🚀'
</div>
<p>A <code>rune</code> can be converted to UTF-8 bytes by using the <code>.bytes()</code> method.</p>
<div class="language-v">rocket := `🚀`
assert rocket.bytes() == [u8(0xf0), 0x9f, 0x9a, 0x80]
</div>
<p>Hex, Unicode, and Octal escape sequences also work in a <code>rune</code> literal:</p>
<div class="language-v">assert `\x61` == `a`
assert `\141` == `a`
assert `\u0061` == `a`
// multibyte literals work too
assert `\u2605` == `★`
assert `\u2605`.bytes() == [u8(0xe2), 0x98, 0x85]
assert `\xe2\x98\x85`.bytes() == [u8(0xe2), 0x98, 0x85]
assert `\342\230\205`.bytes() == [u8(0xe2), 0x98, 0x85]
</div>
<p>Note that <code>rune</code> literals use the same escape syntax as strings, but they can only hold one unicode
character. Therefore, if your code does not specify a single Unicode character, you will receive an
error at compile time.</p>
<p>Also remember that strings are indexed as bytes, not runes, so beware:</p>
<div class="language-v">rocket_string := '🚀'
assert rocket_string[0] != `🚀`
assert 'aloha!'[0] == `a`
</div>
<p>A string can be converted to runes by the <code>.runes()</code> method.</p>
<div class="language-v">hello := 'Hello World 👋'
hello_runes := hello.runes() // [`H`, `e`, `l`, `l`, `o`, ` `, `W`, `o`, `r`, `l`, `d`, ` `, `👋`]
assert hello_runes.string() == hello
</div>
<h2 id="numbers">Numbers <a href="v-types.html#numbers" class="header-anchor" aria-hidden="true">#</a></h2>
<div class="language-v">a := 123
</div>
<p>This will assign the value of 123 to <code>a</code>. By default <code>a</code> will have the
type <code>int</code>.</p>
<p>You can also use hexadecimal, binary or octal notation for integer literals:</p>
<div class="language-v">a := 0x7B
b := 0b01111011
c := 0o173
</div>
<p>All of these will be assigned the same value, 123. They will all have type
<code>int</code>, no matter what notation you used.</p>
<p>V also supports writing numbers with <code>_</code> as separator:</p>
<div class="language-v">num := 1_000_000 // same as 1000000
three := 0b0_11 // same as 0b11
float_num := 3_122.55 // same as 3122.55
hexa := 0xF_F // same as 255
oct := 0o17_3 // same as 0o173
</div>
<p>If you want a different type of integer, you can use casting:</p>
<div class="language-v">a := i64(123)
b := u8(42)
c := i16(12345)
</div>
<p>Assigning floating point numbers works the same way:</p>
<div class="language-v">f := 1.0
f1 := f64(3.14)
f2 := f32(3.14)
</div>
<p>If you do not specify the type explicitly, by default float literals
will have the type of <code>f64</code>.</p>
<p>Float literals can also be declared as a power of ten:</p>
<div class="language-v">f0 := 42e1 // 420
f1 := 123e-2 // 1.23
f2 := 456e+2 // 45600
</div>
<h2 id="arrays">Arrays <a href="v-types.html#arrays" class="header-anchor" aria-hidden="true">#</a></h2>
<p>An array is a collection of data elements of the same type. An array literal is a
list of expressions surrounded by square brackets. An individual element can be
accessed using an <em>index</em> expression. Indexes start from <code>0</code>:</p>
<div class="language-v">mut nums := [1, 2, 3]
println(nums) // `[1, 2, 3]`
println(nums[0]) // `1`
println(nums[1]) // `2`
nums[1] = 5
println(nums) // `[1, 5, 3]`
</div>
<p><a id='array-operations'></a></p>
<p>An element can be appended to the end of an array using the push operator <code><<</code>.
It can also append an entire array.</p>
<div class="language-v">mut nums := [1, 2, 3]
nums << 4
println(nums) // "[1, 2, 3, 4]"
// append array
nums << [5, 6, 7]
println(nums) // "[1, 2, 3, 4, 5, 6, 7]"
</div>
<div class="language-v">mut names := ['John']
names << 'Peter'
names << 'Sam'
// names << 10 <-- This will not compile. `names` is an array of strings.
</div>
<p><code>val in array</code> returns true if the array contains <code>val</code>. See <a href="statements-&-expressions.html#in-operator"><code>in</code> operator</a>.</p>
<div class="language-v">names := ['John', 'Peter', 'Sam']
println('Alex' in names) // "false"
</div>
<h3 id="array-fields">Array Fields <a href="v-types.html#array-fields" class="header-anchor" aria-hidden="true">#</a></h3>
<p>There are two fields that control the "size" of an array:</p>
<ul>
<li><code>len</code>: <em>length</em> - the number of pre-allocated and initialized elements in the array</li>
<li><code>cap</code>: <em>capacity</em> - the amount of memory space which has been reserved for elements,
but not initialized or counted as elements. The array can grow up to this size without
being reallocated. Usually, V takes care of this field automatically but there are
cases where the user may want to do manual optimizations (see <a href="v-types.html#array-initialization">below</a>).</li>
</ul>
<div class="language-v">mut nums := [1, 2, 3]
println(nums.len) // "3"
println(nums.cap) // "3" or greater
nums = [] // The array is now empty
println(nums.len) // "0"
</div>
<p><code>data</code> is a field (of type <code>voidptr</code>) with the address of the first
element. This is for low-level <a href="memory-unsafe-code.html"><code>unsafe</code></a> code.</p>
<blockquote class="tip">
<p>[!NOTE]
Fields are read-only and can't be modified by the user.</p>
</blockquote>
<h3 id="array-initialization">Array Initialization <a href="v-types.html#array-initialization" class="header-anchor" aria-hidden="true">#</a></h3>
<p>The type of an array is determined by the first element:</p>
<ul>
<li><code>[1, 2, 3]</code> is an array of ints (<code>[]int</code>).</li>
<li><code>['a', 'b']</code> is an array of strings (<code>[]string</code>).</li>
</ul>
<p>The user can explicitly specify the type for the first element: <code>[u8(16), 32, 64, 128]</code>.
V arrays are homogeneous (all elements must have the same type).
This means that code like <code>[1, 'a']</code> will not compile.</p>
<p>The above syntax is fine for a small number of known elements but for very large or empty
arrays there is a second initialization syntax:</p>
<div class="language-v">mut a := []int{len: 10000, cap: 30000, init: 3}
</div>
<p>This creates an array of 10000 <code>int</code> elements that are all initialized with <code>3</code>. Memory
space is reserved for 30000 elements. The parameters <code>len</code>, <code>cap</code> and <code>init</code> are optional;
<code>len</code> defaults to <code>0</code> and <code>init</code> to the default initialization of the element type (<code>0</code>
for numerical type, <code>''</code> for <code>string</code>, etc). The run time system makes sure that the
capacity is not smaller than <code>len</code> (even if a smaller value is specified explicitly):</p>
<div class="language-v">arr := []int{len: 5, init: -1}
// `arr == [-1, -1, -1, -1, -1]`, arr.cap == 5
// Declare an empty array:
users := []int{}
</div>
<p>Setting the capacity improves performance of pushing elements to the array
as reallocations can be avoided:</p>
<div class="language-v">mut numbers := []int{cap: 1000}
println(numbers.len) // 0
// Now appending elements won't reallocate
for i in 0 .. 1000 {
numbers << i
}
</div>
<blockquote class="tip">
<p>[!NOTE]
The above code uses a <a href="statements-&-expressions.html#range-for">range <code>for</code></a> statement.</p>
</blockquote>
<p>You can initialize the array by accessing the <code>index</code> variable which gives
the index as shown here:</p>
<div class="language-v">count := []int{len: 4, init: index}
assert count == [0, 1, 2, 3]
mut square := []int{len: 6, init: index * index}
// square == [0, 1, 4, 9, 16, 25]
</div>
<h3 id="array-types">Array Types <a href="v-types.html#array-types" class="header-anchor" aria-hidden="true">#</a></h3>
<p>An array can be of these types:</p>
<table>
<thead>
<tr>
<th>Types</th>
<th>Example Definition</th>
</tr>
</thead>
<tbody>
<tr>
<td>Number</td>
<td><code>[]int,[]i64</code></td>
</tr>
<tr>
<td>String</td>
<td><code>[]string</code></td>
</tr>
<tr>
<td>Rune</td>
<td><code>[]rune</code></td>
</tr>
<tr>
<td>Boolean</td>
<td><code>[]bool</code></td>
</tr>
<tr>
<td>Array</td>
<td><code>[][]int</code></td>
</tr>
<tr>
<td>Struct</td>
<td><code>[]MyStructName</code></td>
</tr>
<tr>
<td>Channel</td>
<td><code>[]chan f64</code></td>
</tr>
<tr>
<td>Function</td>
<td><code>[]MyFunctionType</code> <code>[]fn (int) bool</code></td>
</tr>
<tr>
<td>Interface</td>
<td><code>[]MyInterfaceName</code></td>
</tr>
<tr>
<td>Sum Type</td>
<td><code>[]MySumTypeName</code></td>
</tr>
<tr>
<td>Generic Type</td>
<td><code>[]T</code></td>
</tr>
<tr>
<td>Map</td>
<td><code>[]map[string]f64</code></td>
</tr>
<tr>
<td>Enum</td>
<td><code>[]MyEnumType</code></td>
</tr>
<tr>
<td>Alias</td>
<td><code>[]MyAliasTypeName</code></td>
</tr>
<tr>
<td>Thread</td>
<td><code>[]thread int</code></td>
</tr>
<tr>
<td>Reference</td>
<td><code>[]&f64</code></td>
</tr>
<tr>
<td>Shared</td>
<td><code>[]shared MyStructType</code></td>
</tr>
</tbody>
</table>
<p><strong>Example Code:</strong></p>
<p>This example uses <a href="structs.html">Structs</a> and <a href="type-declarations.html#sum-types">Sum Types</a> to create an array
which can handle different types (e.g. Points, Lines) of data elements.</p>
<div class="language-v">struct Point {
x int
y int
}
struct Line {
p1 Point
p2 Point
}
type ObjectSumType = Line | Point
mut object_list := []ObjectSumType{}
object_list << Point{1, 1}
object_list << Line{
p1: Point{3, 3}
p2: Point{4, 4}
}
dump(object_list)
/*
object_list: [ObjectSumType(Point{
x: 1
y: 1
}), ObjectSumType(Line{
p1: Point{
x: 3
y: 3
}
p2: Point{
x: 4
y: 4
}
})]
*/
</div>
<h3 id="multidimensional-arrays">Multidimensional Arrays <a href="v-types.html#multidimensional-arrays" class="header-anchor" aria-hidden="true">#</a></h3>
<p>Arrays can have more than one dimension.</p>
<p>2d array example:</p>
<div class="language-v">mut a := [][]int{len: 2, init: []int{len: 3}}
a[0][1] = 2
println(a) // [[0, 2, 0], [0, 0, 0]]
</div>
<p>3d array example:</p>
<div class="language-v">mut a := [][][]int{len: 2, init: [][]int{len: 3, init: []int{len: 2}}}
a[0][1][1] = 2
println(a) // [[[0, 0], [0, 2], [0, 0]], [[0, 0], [0, 0], [0, 0]]]
</div>
<h3 id="array-methods">Array methods <a href="v-types.html#array-methods" class="header-anchor" aria-hidden="true">#</a></h3>
<p>All arrays can be easily printed with <code>println(arr)</code> and converted to a string
with <code>s := arr.str()</code>.</p>
<p>Copying the data from the array is done with <code>.clone()</code>:</p>
<div class="language-v">nums := [1, 2, 3]
nums_copy := nums.clone()
</div>
<p>Arrays can be efficiently filtered and mapped with the <code>.filter()</code> and
<code>.map()</code> methods:</p>
<div class="language-v">nums := [1, 2, 3, 4, 5, 6]
even := nums.filter(it % 2 == 0)
println(even) // [2, 4, 6]
// filter can accept anonymous functions
even_fn := nums.filter(fn (x int) bool {
return x % 2 == 0
})
println(even_fn)
</div>
<div class="language-v">words := ['hello', 'world']
upper := words.map(it.to_upper())
println(upper) // ['HELLO', 'WORLD']
// map can also accept anonymous functions
upper_fn := words.map(fn (w string) string {
return w.to_upper()
})
println(upper_fn) // ['HELLO', 'WORLD']
</div>
<p><code>it</code> is a builtin variable which refers to the element currently being
processed in filter/map methods.</p>
<p>Additionally, <code>.any()</code> and <code>.all()</code> can be used to conveniently test
for elements that satisfy a condition.</p>
<div class="language-v">nums := [1, 2, 3]
println(nums.any(it == 2)) // true
println(nums.all(it >= 2)) // false
</div>
<p>There are further built-in methods for arrays:</p>
<ul>
<li><code>a.repeat(n)</code> concatenates the array elements <code>n</code> times</li>
<li><code>a.insert(i, val)</code> inserts a new element <code>val</code> at index <code>i</code> and
shifts all following elements to the right</li>
<li><code>a.insert(i, [3, 4, 5])</code> inserts several elements</li>
<li><code>a.prepend(val)</code> inserts a value at the beginning, equivalent to <code>a.insert(0, val)</code></li>
<li><code>a.prepend(arr)</code> inserts elements of array <code>arr</code> at the beginning</li>
<li><code>a.trim(new_len)</code> truncates the length (if <code>new_length < a.len</code>, otherwise does nothing)</li>
<li><code>a.clear()</code> empties the array without changing <code>cap</code> (equivalent to <code>a.trim(0)</code>)</li>
<li><code>a.delete_many(start, size)</code> removes <code>size</code> consecutive elements from index <code>start</code>
– triggers reallocation</li>
<li><code>a.delete(index)</code> equivalent to <code>a.delete_many(index, 1)</code></li>
<li><code>a.delete_last()</code> removes the last element</li>
<li><code>a.first()</code> equivalent to <code>a[0]</code></li>
<li><code>a.last()</code> equivalent to <code>a[a.len - 1]</code></li>
<li><code>a.pop()</code> removes the last element and returns it</li>
<li><code>a.reverse()</code> makes a new array with the elements of <code>a</code> in reverse order</li>
<li><code>a.reverse_in_place()</code> reverses the order of elements in <code>a</code></li>
<li><code>a.join(joiner)</code> concatenates an array of strings into one string
using <code>joiner</code> string as a separator</li>
</ul>
<p>See all methods of <a href="https://modules.vlang.io/index.html#array">array</a></p>
<p>See also <a href="https://modules.vlang.io/arrays.html">vlib/arrays</a>.</p>
<h4 id="sorting-arrays">Sorting Arrays <a href="v-types.html#sorting-arrays" class="header-anchor" aria-hidden="true">#</a></h4>
<p>Sorting arrays of all kinds is very simple and intuitive. Special variables <code>a</code> and <code>b</code>
are used when providing a custom sorting condition.</p>
<div class="language-v">mut numbers := [1, 3, 2]
numbers.sort() // 1, 2, 3
numbers.sort(a > b) // 3, 2, 1
</div>
<div class="language-v">struct User {
age int
name string
}
mut users := [User{21, 'Bob'}, User{20, 'Zarkon'}, User{25, 'Alice'}]
users.sort(a.age < b.age) // sort by User.age int field
users.sort(a.name > b.name) // reverse sort by User.name string field
</div>
<p>V also supports custom sorting, through the <code>sort_with_compare</code> array method.
Which expects a comparing function which will define the sort order.
Useful for sorting on multiple fields at the same time by custom sorting rules.