3 ms·
You make it sound so difficult, but in my experience it hardly ever comes up in practice. When it does, rather than naming things obscurely I can just tack a te
by AnIdiotOnTheNet 4y ago
You make it sound so difficult, but in my experience it hardly ever comes up in practice. When it does, rather than naming things obscurely I can just tack a temp_ or local_ or something else descriptive on the front.
- jeltz 4y agoWhen 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.
- cztomsik 4y agoIt's accidental complexity. There's an artificial limitation which you need to work around. What you get back is very questionable, I certainly don't remember when it was last time I've messed up the scoping, if ever.