I’ve been writing a small program (that I’m hoping to showcase soon!) that needs to walk a directory tree to do its job.
The basic code I want to write is as follows:
while (walker.next()) |ok| {
const entry = ok orelse break; // walker is finished
// do stuff with entry...
} else |err| {
switch (err) {
// handle errors, mostly by just printing them
}
continue;
}
But it doesn’t compile, because the continue in the final line is not considered to be in a loop!
I’ve tried handling errors inside the condition, but that lead me to multiple layers of while loops to avoid the terminate-on-error behavior and really seemed excessively large and rather unreadable for what it was trying to achieve.
A second-best sort of solution I thought up was to somehow invert the optional and error (turning Error!?Entry into ?Error!Entry) writing the loop like so:
while (invert(walker.next())) |err| {
const entry = err catch |e| switch (e) {
// handle errors, mostly by just printing them
}
// do stuff with entry...
}
But although the invert() function does seem like it will work, at this point I started getting the feeling that I’m on the wrong track altogether here and am missing a much simpler solution. Is there a better way, or is this the way to go about it?
vulpesx July 11, 2026, 12:57pm 2
while (true) {
// not sure if () are needed...
const entry = (walker.next() catch |e| switch (e) {
//...
}) orelse break;
// or perhaps split into 2 consts
}
Simple as that, huh?
It works great, thanks a lot!
0 Comments
Log in to join the conversation.No comments yet. Be the first to share your thoughts.