Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

Rust doesn't have Zig's comptime feature. Rust's const fn's are normal functions that are capable of running at compile time. It's an optional optimisation; it doesn't exist any additional semantic capabilities because they also need to be able to run at runtime.

Zig's comptime functions only run at compile time, so they can do extra things - in particular manipulating types - that you can't do if your function needs to run at runtime. (Don't mention dependent types.)



Note that dependently-typed code also effectively "runs at compile-time", it's inherent to that programming model. You can "extract" an ordinary program from dependently-typed code which you can then compile to a binary and run as usual, but then that program will not feature dependent types in their full generality.


Okay, so this crate adds Zig's actual comptime?


No, it doesn't because it is based on Rust macros which are strictly less capable than Zig comptime in a crucial way (compile time reflection).

Neither Rust macros nor const fn are 100% what Zig comptime is but they have other properties that Zig comptime lacks. Apples and oranges.


Then the title is misleading (although not surprising).

So what are the major differences between the two anyways? Just to be sure.


> Zig's comptime functions only run at compile time, so they can do extra things - in particular manipulating types - that you can't do if your function needs to run at runtime.

Careful, I'm not sure this is true. I haven't found a Zig comptime function that doesn't also work just as well at runtime function.

This is, in fact, the primary characteristic that makes Zig comptime easier to reason about than any "macro" system. If something is wrong in my comptime function, I can normally make a small adjustment to force it to be a runtime function that I can step through and probe and debug.

It's sort of a unification of compile time and run time semantics and it is long overdue. The late John Shutt's Scheme-alike Kernel (https://web.cs.wpi.edu/~jshutt/kernel.html) sort of approached this as did old-school Tcl.


Correcting myself: Any Zig function which returns a Type can only be run at comptime as a Type can only be created at compile time.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: