Hacker News new | past | comments | ask | show | jobs | submit login

I was wondering recently if "comptime for()" wouldn't be a better name, because "inline for()" sounds like its main feature is "performance through loop unrolling", but the actual main feature seems to be "comptime-duck-typing through loop unrolling" :)



`comptime for (someslice) |capture| {};` is already a valid expression so it would conflict along with `inline while` being the other form.

The majority of uses of it in unrolling duck-typing cases will most likely be replaced by [1] which allows the same but with `switch` instead. Thus `inline for` will be left for loop unrolling and possibly in use with `inline switch` where it matters.

[1]: https://github.com/ziglang/zig/issues/7224


Absolutely. I don't know Zig and was confused why inlining a loop would help. `comptime for` is 10 times clearer.


The problem is that comptime implies a full compile-time evaluation, while when doing an inline for you only want to unroll over a list of potentially heterogeneous elements but the body of the for loop will remain available for evaluation at runtime, if not all vales are comptime known. In a comptime block that would cause a compile error.

I have an example of that in this blog post: https://kristoff.it/blog/what-is-zig-comptime/


Right but it's just the `for` bit that's `comptime` right? The loop is unrolled and the loop condition must be `comptime`.

It makes sense to me that `comptime for` means that the `for` is `comptime` but the body of the loop isn't.




Consider applying for YC's Spring batch! Applications are open till Feb 11.

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

Search: