Ziggit

Use compile-time code to initialize an array (opens in new tab)

The code in question is in the global scope (outside any functions) and it’s initializing a global variable. Zig doesn’t have C++'s concept of executing runtime code before main() (and thank god for that), so the code will run at comptime, even if not annotated with comptime. PS: e.g. if you sift through all the noise here: Compiler Explorer, you’ll find the ‘comptime-populated’ array: example.fancy_array: .long 0 .long 3 .long 6 .long 9 .long 12 .long 15 .long 18 .long 21 .long 24 .long 27

Read the original article
Sign in to keep reading the full article.

Keyboard Shortcuts

Navigation

Next / previous post
j/k
Open post
oorEnter
Preview post
v

Post Actions

Love post
a
Like post
l
Dislike post
d
Undo reaction
u
Save / unsave
s

Recommendations

Add interest / feed
Enter
Not interested
x

Go to

Home
gh
Interests
gi
Feeds
gf
Likes
gl
History
gy
Changelog
gc
Settings
gs
Discover
gb
Search
/

General

Show this help
?
Submit feedback
!
Close modal / unfocus
Esc

Press ? anytime to show this help