6 ms·
The biggest issue I have with this proposal is that reading the code in naïve fashion comes up with the wrong answer for me; YMMV. The proposed form-- foo ?
by floating-io 2y ago
The biggest issue I have with this proposal is that reading the code in naïve fashion comes up with the wrong answer for me; YMMV. The proposed form--
foo ? { bar }
Reads naturally to me as "If foo then bar", when it's actually "If foo's error return exists then bar". I would suggest a different operator character, because this one reads wrongly IMO.
Maybe it's just because I originally come from C, where `foo ? bar : baz` meant "if foo then bar else baz", but the fact remains...
- jayd16 2y agoHow about this? :) { foo } catch { bar }
- nonethewiser 2y agoThis is probably the one that would anger Golangers the most lol
- jayd16 2y agoThey could even have typed error checking for different errors and an automatic return error syntax. { Foo } catch (err) {} catch (err2){} throw
- 9rx 2y agoAgreed. The reduction in boilerplate to route error conditions is what we really need, if we are going to add any special error concept. errors.Is/errors.As and the accompanying switch/if statements can quickly consume entire screens of code for all but the most simplest of failure cases and as a result it often sees developers become lazy and just return err instead.
- chairhairair 2y agoIt could be: foo ?! { bar } But, now we’re potentially confusing the “negation” and the “exclamation” meanings the bang symbol usually tries to communicate.
- nonethewiser 2y agoI tend to agree that ? looks like "if then" when what we really want is some sort of coalescing, or "if not then". foo ?? { bar } foo ?/ { bar } foo ?: { bar } foo ?> { bar } foo ||> { bar } Im not sure I like the idea at all though. It seems like a hack around a pretty explicit design choice. Although I do tend to agree the error handling boilerplate is kind of annoying.
- zemo 2y agoGo doesn't have a ? operator today, and the ? operator being used for error handling has precedence in Rust and Zig, so it doesn't seem to be all that out of the ordinary or without precedent in other languages.
- plorkyeran 2y agoThe proposed behavior is the opposite of how Swift and Rust use ?. `foo?.bar()` invokes `foo.bar()` in the non-error case, while in Go `foo ? { bar() }` invokes it in the error case.
- kortex 2y agoThat is exactly how go usually works with error handling though. func mayfail() error { if snafu { return errors.New("oops") } else { return nil} } err := mayfail() if err != nil { handle } Same as `mayfail() ? handle : happypath` would behave with lazy evaluation.
- toxik 2y agofoo else { bar }
- xigoi 2y agoV, which is syntactically similar to Go, uses foo or { bar }