5 ms·
Eventually you'll be able to use std::expected in C++23! https://en.cppreference.com/w/cpp/header/expected https://en.cppreference.com/w/cpp/header/expected D
by jgavris 4y ago
Eventually you'll be able to use std::expected in C++23!
https://en.cppreference.com/w/cpp/header/expected https://en.cppreference.com/w/cpp/header/expected
Don't throw exceptions, require the caller to handle errors and propagate them up the stack (everything returns an expected) if they cannot be handled. You are forced to model the error domains instead of just throwing an exception and assuming the caller knows to catch it and do something with it.
Java has checked exceptions, but, Kotlin decided to abandon them.
The nice codebases I have worked on stick to the Result<T,E> type in Swift or Kotlin. And thus you are forced to 'translate' errors (exceptions?) as described in Alan Griffith's 'Exceptional Java'.
https://accu.org/journals/overload/10/48/griffiths_406/ https://accu.org/journals/overload/10/48/griffiths_406/
"If a checked exception is thrown (to indicate an operation failure) by a method in one package it is not to be propagated by a calling method in a second package. Instead the exception is caught and "translated". Translation converts the exception into: an appropriate return status for the method, a checked exception appropriate to the calling package or an unchecked exception recognised by the system. (Translation to another exception type frequently involves "wrapping".)"
If you can't wait for C++23, there's a single header implementation here.
https://github.com/TartanLlama/expected https://github.com/TartanLlama/expected
- deleted 4y ago[deleted]
- rich_sasha 4y agoI last used C++ extensively in the 00s but my general impression of modern C++ is that as it becomes more and more complex, it tackles it's complexity... by adding more complexity.
- primeblue 4y ago
- DoctorDabadedoo 4y agoThat's exactly how it is, every few years there is so much added without taking almost nothing back that it feels a different language, with so many styles out there it's challenging, to say the least.
- pjmlp 4y agoJust like any other language, Python 3 will be a study case for generations for what happens when one takes features away.
- benreesman 4y agoPretty debatable. C++ was always a pretty big language. Maintaining a decades-old project is going to require either ongoing modernization or you’ll most likely end up touching a lot of it, but you can, and the “start over” option proposed so frequently around here is not for organizations with tens of thousands of engineer years in their codebase. New stuff that doesn’t need your decade-plus of C++ libraries or hit a few other corner cases should probably be in Rust, but you’re back to, that’s mostly greenfield stuff. If you are beholden to not starting from scratch, modern C++ heavily subsets the language and is rather pleasant, certainly the tooling is best-in-class (which is often overlooked). I consider it a good thing that C++ still compiles code from decades ago, and it’s frankly astonishing how well it’s aged when you consider the domains it targets. Ditching everything every few years is nice work if you can get it, and this being HN that’s going to be a vocal bloc, but usually you get to greenfield everything because something else failed.
- scaredginger 4y agoThere is also Boost Outcome
- nextaccountic 4y agoDoes C++23 have something like Rust's ? for std::expected?
- tsimionescu 4y agoExceptions.
- dureuill 4y agoExceptions are nothing like Rust's Result. - exceptions don't appear in the signature of a function - you don't have to handle exceptions explicitly, while you cannot access the value of a Result without explicit error handling - Exceptions are implemented like Rust's panic. The error path in exception is more costly, while the nominal path is supposedly less so, due to less register and branch pressure than the result type that at least has a discriminant to check. Exceptions are such a different way or handling errors from Result that my team finds itself reimplementing Result types in C++ again and again. Alas, it is neither standard nor ergonomic in C++
- lostdog 4y agoIf compound statements every get standardized then you could write a TRY macro that worked pretty similarly. https://gcc.gnu.org/onlinedocs/gcc/Statement-Exprs.html https://gcc.gnu.org/onlinedocs/gcc/Statement-Exprs.html
- benreesman 4y ago