-
Notifications
You must be signed in to change notification settings - Fork 6
Implicit Conversion for fine::Ok and fine::Error #13
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Implicit Conversion for fine::Ok and fine::Error #13
Conversation
This rework ensures that `fine::Ok` and `fine::Error` can correctly convert when used in conjunction with container types like `std::variant`. The downside to this approach, as implemented, is the need to go through 2 factory functions, `fine::ok` and `fine::error`, to construct these.
b1422b5
to
bd57c40
Compare
Class Template Argument Deduction allows class templates to be inferred from constructor arguments, which removes the need for `fine::ok` and `fine::error` as special factory functions.
bd57c40
to
016a7d2
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good to me, I dropped minor notes :)
std::variant<fine::Ok<int64_t>, fine::Error<std::string>> find_meaning(ErlNifEnv *env) { | ||
if (...) { | ||
return fine::Error<std::string>("something went wrong"); | ||
std::variant<fine::Ok<int64_t, int64_t>, fine::Error<std::string>> divmod(ErlNifEnv *env, int64_t a, int64_t b) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice!
c_include/fine.hpp
Outdated
template <typename = std::enable_if_t<std::is_default_constructible_v<Items>>> | ||
Ok() : m_items() {} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We know that std::tuple<>()
is constructible, so we don't need this conditional?
template <typename = std::enable_if_t<std::is_default_constructible_v<Items>>> | |
Ok() : m_items() {} | |
Ok() : m_items() {} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A remnant from trying to understand and make the template code works. I though I still needed it.
It is analogous to Ok<Args...>::Ok(Args...)
with an empty parameter pack.
Also, keeping this would enable constructing like so:
return fine::Ok<std::string> {};
Which I don't think is wanted.
This rework ensures that
fine::Ok
andfine::Error
can correctly convert when used in conjunction with container types likestd::variant
.This is an alternative to #12. Instead of exposing a result type, we ensure that conversions when
fine::Ok
andfine::Error
withstd::variant
work as expected.The downside to this implementation is the need to go through 2 factory functions,fine::ok
andfine::error
, to construct these correctly. I believe this problem could be solved, like CTAD is applicable to tuples, but it would require a lot more template metaprogramming to get everything working correctly.