3 ms·
I can give a specific example. for (0..5) |i| { i = i + 1; std.debug.print("foo {}\n", .{i}); } In this loop in Zig, the reassignment
by Zambyte 1y ago
I can give a specific example.
for (0..5) |i| {
i = i + 1;
std.debug.print("foo {}\n", .{i});
}
In this loop in Zig, the reassignment to i fails, because i is a constant. However, i is a new constant bound to a different value each iteration.
To potentially make it clearer that this is not mutation of a constant between iterations, technically &i could change between iterations, and the program would still be correct. This is not true with a c-style for loop using explicit mutation.
- skeezyjefferson 1y agoI argue in your example there are 6 constants, not 1 constant with 6 different values, though this could be semantics ie we could both be right in some way
- Zambyte 1y agoExactly. As said higher in this comment chain: > So, constant, but repeatedly redefined.
- skeezyjefferson 11mo agoit was constant if it could have been redefined, by the basic definition of what constant means. so no, not constant, but constants