4 ms·
It seems that `inline for` is the corner of `comptime` that newcomers appreciate the most. See this other example: https://clips.twitch.tv/RockyLivelyChimpanze
by kristoff_it 6y ago
It seems that `inline for` is the corner of `comptime` that newcomers appreciate the most. See this other example:
https://clips.twitch.tv/RockyLivelyChimpanzeeDoubleRainbow-Pov6fVYDbH1_r-A6 https://clips.twitch.tv/RockyLivelyChimpanzeeDoubleRainbow-P...
Full video available here:
https://www.youtube.com/watch?v=O4UYT-brgrc https://www.youtube.com/watch?v=O4UYT-brgrc
- flohofwoe 6y agoI 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" :)
- IshKebab 6y agoAbsolutely. I don't know Zig and was confused why inlining a loop would help. `comptime for` is 10 times clearer.
- kristoff_it 6y agoThe 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/ https://kristoff.it/blog/what-is-zig-comptime/
- IshKebab 6y agoRight 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.
- nyberg 6y ago`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 https://github.com/ziglang/zig/issues/7224