oktal / result Goto Github PK
View Code? Open in Web Editor NEWMy implementation of a C++ Result<T, E> type inspired by Rust
License: Apache License 2.0
My implementation of a C++ Result<T, E> type inspired by Rust
License: Apache License 2.0
Currently there is no mention of how you wish to license this software. Therefore, technically (under some interpretations), it is in violation of copyright law for others to make a copy.
I'd think it'd be nice for the community if you explicitly set the license.
For a single-file header, I'd recommend a very permissive license, such as the UNLICENSE or MIT.
The project looks good, thanks!
Currently several functions will call std::terminate
on failure. It would be beneficial to allow users to customize this behavior, or possibly switch to using an exception (e.g., illegal state) so the user can catch it and then decide what to do next. While this would not be ideal, there may be circumstances where terminating is extremely inappropriate as there won't be a chance to do a proper cleanup.
Right now this library contains two versions of Result
: one with normal Ok
enum and another with void
one.
It may be a short-term convenience for callers (simple Ok() suffices if there's nothing of value to return). However:
void
is an incomplete type and thus presents distant problems for callers (like #6)Which begs a question: do we really need special casing for void
? My proposal is:
void
from Ok
std::tuple<>
) / empty struct (struct {}
) and relevant Ok (using OK = Ok<Dummy>
)DUMMY_CB
for [&](const Dummy&)
This will lead to a closer-to-Rust Ok construction in the form of OK({})
and eliminate any void-related template issues.
Could you add an example for mapError function usage?
Lines 862 to 873 in fee9af7
Shouldn't a simple return false;
in the end fix it?
template<typename R, typename E>
class
#if __cplusplus >= 201703L
[[nodiscard]]
#endif
Result {
};
This is c++17 feature and will show warning if you will ignore the Result
It seems the only way to retrieve the value within the Err
is to call through to the storage struct. In Rust, this is done with a match
, but there's nothing exactly like that in C++. Could you add an unwrapErr
or add documentation on the proper way to extract values like you would with match
in Rust?
namespace concept
is invalid in C++20-enabled compilers because concept
is then a keyword.
TRY macro fails when we have void
associated with Ok (and rightfully so, since assigning void makes no sense).
Perhaps a separate macro for simple error propagation and static_assert for more informative failure?
in expansion of macro 'TRY'
result.h:631:14: note: candidate: template<class U> const U& details::Storage<void, E>::get() const [with U = U; E = int]
const U& get() const {
^~~
result.h:631:14: note: template argument deduction/substitution failed:
result.h: In substitution of 'template<class U> const U& details::Storage<void, E>::get() const [with U = void]':
result.h:631:14: error: forming reference to void
result.h:636:8: note: candidate: template<class U> U& details::Storage<void, E>::get() [with U = U; E = int]
U& get() {
^~~
result.h:636:8: note: template argument deduction/substitution failed:
result.h: In substitution of 'template<class U> U& details::Storage<void, E>::get() [with U = void]':
result.h:636:8: error: forming reference to void
Is it still possible to use andThen/bind ?
"then" is not what i'm looking for (i think).
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.