What if we extend the .? operator to work with Enums, expecially for Errors #5320
Replies: 2 comments 3 replies
-
|
This syntactic change would make you type less characters when handling errors, but beyond that there's little value in it. You still have to write the So really, there isn't logically different from what we were doing before, it's just syntax sugar for the sake of optimizing typing extra 5 characters. These types of changes don't affect readability too much (except for when people start preferring one way of doing things over the other and suddenly it's not so easy to skim the code for the error handling snippets), and their sole purpose is to allow you to type less. If you type fast, you don't really care, because you aren't saving much time. If you type slow, you still don't care because at the end most of the time you spend on programming is spend thinking, and if you're not making the logic easy, you're not optimizing that thinking time. This is in contrast to what That said, we kind of already have a thing that helps with do_stuff :: proc() -> (value: int, err: Error) {
value = do_other_stuff() or_return
return value, nil
}Unfortunately yes, if there is an error you can't easily get it back with these operators and do something like |
Beta Was this translation helpful? Give feedback.
-
|
No.
Also, did you really suggest |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Right now we have the .? operator that works with unions with 1 variant ( Almost exclusively for Maybe($T) ) which returns two values: the underlying value of that type, and a bool which will be true if the union does not equal to nil.
What if we just pull the bool checking from .? to use with Enums so now we can do stuff like:
I think this will help with the boilerplate "err != nil" and also make the .? more usable. Me personally I don't mind the err != nil syntax and think that the error control flow is already great without the try/catch or stuff like that. The only problem I see with this approach is the readability (only for the first time exposing to this) and also the .? would be less orthogonal.
Thoughts?
Beta Was this translation helpful? Give feedback.
All reactions