https://news.ycombinator.com/item?id=44432640
It’s actually my #1 issue. I hate not knowing about error conditions and no one in Java uses checked exceptions because the language syntax for dealing with them sucks. Brian had a proposal for handling exceptions in switch but it seems to have died in the water.
Part of me secretly hopes Swift takes over the world because they have a typed throws that works and handling errors is a breeze.
let i: Int?
do {
y = try someThrowingFunc()
} catch SomeError.err {
y = nil
} catch SomeError.youCareAbout {
//
}This has consequences for config parsing, for example, where a particular subsection (sub-object? keyed struct?) may be optional, so it being missing is perfectly legal. But if you use try?, then there's no way to distinguish between it simply being missing and it being present but malformed. It unfortunately seems like the only other options in Swift is to propagate the error, or revert back to verbose try-catch blocks.
Whereas, in Zig, you can do inline catching and care about the error only as much as you want to, for example:
// This is equivalent to Swift's try
const i: ?i32 = try someThrowingFunc();
// This is equivalent to Swift's try?
const i: ?i32 = someThrowingFunc() catch null;
// This is a yielding inline catch block that does not care about the error
const i: ?i32 = someThrowingFunc() catch brk: {
logger.warn("Something went wrong!");
break :brk null;
}
It's not perfect, I don't love the block-label-break thing, but I much prefer this if only because then the variable can be defensively const, which I do not believe is possible with the kind of code snippet you provided. Also, instead of breaking out, it could capture and return the error or return a new error. It's incredibly versatile.