
What Is Zig's Comptime? - luu
https://kristoff.it/blog/what-is-zig-comptime/
======
olodus
I love Zig and have played around a bit with it this summer. I do really wish
the standard library was better documented though. Think I saw that would be
coming soon though so i hope I'll have a even better time with it when I come
back to it next time. I would call myself an intermediate programmer with ok
knowledge of C and it was for me sometimes hard to understand how I would do
some stuff in Zig. Most things are very much the same but with some small
changes that I understand why they are there but trip over the differences
when trying to do certain things. Besides stdlib documentation I would also
want some better examples, but that is probably something that cones with
larger adoption. I find Zig together with Rust as a really strong combo of
languages that makes one hopeful for the future of system programing.

------
Siyo
While comptime looks extremely powerful, I'm really not a fan of how it's used
for unconstrained generics. This is the same problem I have with C++ templates
where an incorrect use of a generic function would result in an explosion of
bizarre undescriptive template errors. Sure you can write these type asserts
yourself, but it's time consuming and how many developers will actually do it
and get it right? I don't know, maybe it's really not that big of a deal, but
I much prefer how Rust does this using traits as type constraints (although at
some cost of complexity, e.g. Eq, PartialEq, Ord, PartialOrd). Not to mention
that by using constraints on the type system level, you actually get useful
type signature documentation on what you can or cannot pass to a function.

~~~
pron
For one, Zig's error reporting is more friendly than C++ and will continue to
get better. For another, it's a tradeoff, but the fact that the language is so
much simpler than C++/Rust and compilation faster gives you a lot of headroom
elsewhere (when the development cycle is faster, it's easier to focus your
resources where they matter most _to you_ ).

Of course, language preference is personal and aesthetic, and different people
have different preferences.

~~~
gnuvince
Zig is a simpler language, but that simplicity comes at a cost: the memory
bugs that exist in C also exist in Zig. _Users_ bear the cost of the
programmer using a simple language.

Rust is a more complex language, but it eliminates a number of classes of
memory error. In this case, the programmer bears the cost of users having
safer programs.

Although I find Zig very interesting—and I wish Andy the best of success with
it—the trade-off that the Rust offers is more in line with my values and my
ethics.

~~~
jorangreef
> the memory bugs that exist in C also exist in Zig.

No, they don't. See Zig's release-safe mode.

Also:
[https://news.ycombinator.com/item?id=17184929](https://news.ycombinator.com/item?id=17184929)

> the trade-off that the Rust offers is more in line with my values and my
> ethics.

By the way, it's awesome that you like Rust, but there's no need to imply
there's something ethically wrong with Zig. Depending on the platform you're
targeting, you may yet find a use for either language.

~~~
edflsafoiewq
release-safe doesn't prevent all memory bugs.

~~~
jorangreef
Sure, but then not even "safe" languages like JavaScript will prevent all
memory bugs.

And to be fair, Rust does introduce memory issues of its own in terms of
allocation strategy. For certain projects, Rust would not be considered "safe"
with respect to memory.

Zig's release-safe mode is worlds away from "the memory bugs that exist in C".

See also:
[https://github.com/ziglang/zig/issues/2301](https://github.com/ziglang/zig/issues/2301)

------
andyonthewings
Haxe's macro is similar. It can run arbitrary code during compilation. One
useful thing to do is to get the current git commit hash and inject it into
the program: [https://code.haxe.org/category/macros/add-git-commit-hash-
in...](https://code.haxe.org/category/macros/add-git-commit-hash-in-
build.html)

------
jorangreef
Zig is worth supporting. Andrew Kelley, the author, was recently accepted into
GitHub's Sponsors program. This means for every $1 you contribute, GitHub will
match your donation for the first year:

[https://github.com/users/andrewrk/sponsorship](https://github.com/users/andrewrk/sponsorship)

~~~
DJTtt
That's interesting. Looks like it's a limited beta right now, I wonder if
anyone will be able to enable Patreon-esque sponsorship for their projects in
the future and what the requirements will be. Also very generous of Microsoft
to match donations.

~~~
hellcow
There's nothing generous about it. Microsoft is well behind Patreon and this
is a way to get creators to swap to using GitHub for donations.

~~~
patrickmcnamara
Is there something to stop creators using both?

~~~
hellcow
Not in theory, but in practice why would any creator encourage people to give
them money on a platform that pays out half of the other one? They would push
all people to the one with double payouts.

------
saintfiends
Interesting. I would love to see a similar article on memory management in
Zig.

------
saagarjha
What can Zig _not_ do at compile time?

~~~
mratsim
It cannot do code generation.

~~~
JBReefer
Good!

------
hwj
Considering that many compilers are written in the language they compile I was
surprised by the Zig compiler being written in C++.

Does anybody know the reason?

~~~
euan_torano
The stage 1 compiler is written in C++, but the Stage 2 compiler will be Zig:
[https://github.com/ziglang/zig#stage-2-build-self-hosted-
zig...](https://github.com/ziglang/zig#stage-2-build-self-hosted-zig-from-zig-
source-code)

Eventually, i'll be Zig all the way down, but the language needs to get to the
point where it can actually support writing a compiler for itself (and Andrew
has some big plans for the way the compiler should work from what I
understand).

~~~
floatboth
Hopefully stage1 will remain indefinitely for bootstrapping, like LDC's
ltsmaster.

