4 ms·
Regarding error handling verbosity, you don't actually have to write _ = file.write(data) catch |err| { silence_err_somehow(err); ... }. You can just write
by anonymoushn 5y ago
Regarding error handling verbosity, you don't actually have to write
_ = file.write(data) catch |err| { silence_err_somehow(err); ... }.
You can just write
_ = file.write(data) catch { ... }.
And the first example of using `catch` in the docs[0] does this. It may be useful to also use the non-error return value of `write` though, because partial writes will return an amount of written bytes less than the amount you asked to write. You need `writeAll` if you want to ignore this concern, and then you don't have to write `_ = ` because the non-error part of the error union is void (so you don't have to use it). Then you get
file.writeAll(data) catch { ... }
[0]: https://ziglang.org/documentation/master/#catch https://ziglang.org/documentation/master/#catch
- nonsequitur 5y agoHint: It's `catch {}` instead of `catch { ... }`.
- anonymoushn 5y agoThat's right if you want to do nothing on error. I think TFA uses `...` to represent the code they would actually want to run if an error happened.
- wyldfire 5y agoThat makes sense. But it's easy to confuse a prose ellipsis with the C++ notation for indicating the wildcard exception acceptance pattern.
- kjs3 5y agoZig is not C++.
- hibbelig 5y agoMay no programming language ever introduce “foo” or “bar” keywords, lest mayhem be upon us.
- planckscnst 5y agoNow I'm thinking there must be some brainfuck-like language called foobar.
- wyldfire 5y agoSorry, I suppose you could infer that but it was not my intent. It was merely my suggestion that someone's new exposure to Zig might be 'tainted' by previous exposure to C++ syntax.
- 0des 5y agoThat's how I've always interpreted it unless its a convo about slices which are few. Everyone knows slices are the bees knees item 9, so not often does it need discussing.
- karmakaze 5y agoOne 'catch' that makes me uncomfortable with Zig is the camel/underscore-insensitivity. How do you find all references of a method? Does your editor/IDE know how to find writeAll when I search for references of write_all? Do you have lint rules that are enforced for a project's source?
- AnIdiotOnTheNet 5y agoUnless something has drastically changed since I last used Zig, it doesn't have the feature you're talking about except in the case of number literals. `write_all` is a completely different identifier from `writeAll`.
- nrclark 5y agoIs is possible you're mixing up Zig and Nim?
- lhorie 5y agoThat's nim[0], not zig [0] https://nim-lang.org/docs/manual.html#lexical-analysis-identifier-equality https://nim-lang.org/docs/manual.html#lexical-analysis-ident...
- elcritch 5y agoCorrect, though the case insensitivity hasn't ever cause me issues in Nim. Searching is easy or at most takes a simple regex. It's actually saved me from a few dumb overshadowing issues (as in "you can't redefine 'is_active' see 'isActive' here").