3 ms·
There is of course { var i: usize = 0; while (i != 10) : (i += 1) { ... }} I.e., you are not obliged to plaster on a newline after every semicolon. Th
by ncmncm 5y ago
There is of course
{ var i: usize = 0; while (i != 10) : (i += 1) {
...
}}
I.e., you are not obliged to plaster on a newline after every semicolon. That is roughly as busy as the equivalent C++ loop,
for (auto i = 0u; i != 10; ++i) {
...
}
but syntax is syntax. You would be much better off with a function that takes a lambda, and pass the body as that lambda. In C++, a loop using that would look like
forloop(10u, [&](auto i) {
...
});
That's not a much shorter than the built-in, but leaves a lot less room for mistakes; i.e., if there was a typo in the built-in, it might be very hard to make yourself see it even knowing it must be there.
I can't find anything to suggest Zig has lambdas. That's a planet-sized hole, nowadays.
(BTW, forloop in C++ is just
template <typename Ix, typename Body>
void forloop(Ix top, Body&& body) {
for (Ix i{}; i != top; ++i) body(i);
}
In use it compiles to instructions identical to the built-in loop.)
- pron 5y ago> I can't find anything to suggest Zig has lambdas. Closures are problematic in low-level languages, as their capture behaviour requires tricky syntax and/or implicitness, and Zig is very much opposed to both. Non-capturing anonymous functions expressions will be added (https://github.com/ziglang/zig/issues/1717 https://github.com/ziglang/zig/issues/1717), while a limited form of closure is being considered (https://github.com/ziglang/zig/issues/6965 https://github.com/ziglang/zig/issues/6965).