3 ms·
When I used Zig I ran into this all the time in practice when using iterators. It is the combination of that you cannot nicely create a locally scoped iterator
by jeltz 4y ago
When I used Zig I ran into this all the time in practice when using iterators. It is the combination of that you cannot nicely create a locally scoped iterator and that Zig forbids shadowing.
Specifically this kind of code (and I do not feel calling them e.g. iter1 and iter2 or foo_iter and bar_iter make the code nice):
var iter1 = foo_dir.iterate();
while (try iter1.next()) |entry| {
// Do something
}
var iter2 = bar_dir.iterate();
while (try iter2.next()) |entry| {
// Do something else
}
- messe 4y agoAt the moment, the recommended solution for that is to use a block scope. { var iter = foo_dir.iterate(); while (try iter.next()) |entry| { // Do something } } { var iter = bar_dir.iterate(); while (try iter.next()) |entry| { // Do something } }
- AnIdiotOnTheNet 4y agoBeing clear about things is one of Zig's central philosophies, if naming things `foo_iter` and `bar_iter` doesn't feel like the right solution then maybe the language isn't a good fit for you. But also: var iter = foo_dir.iterate(); while (try iter.next()) |entry| { // Do something } iter = bar_dir.iterate(); while (try iter.next()) |entry| { // Do something else } You can totally just reassign `iter` here since you're done with the first iterator (assuming they are both the same type, which appears to be the case).
- jeltz 4y agoI do not think long variable names is very clear. What is clear is locally scoped ones and there I feel Zig has some deficiencies which makes it unnecessarily cumbersome to have locally scoped iterators.