-
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
Changes from 2 commits
662c321
016a7d2
ebdadfa
77a9b84
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||
---|---|---|---|---|---|---|---|---|
|
@@ -145,23 +145,53 @@ class Term { | |||||||
// Represents a `:ok` tagged tuple, useful as a NIF result. | ||||||||
template <typename... Args> class Ok { | ||||||||
public: | ||||||||
Ok(const Args &...items) : items(items...) {} | ||||||||
using Items = std::tuple<Args...>; | ||||||||
|
||||||||
private: | ||||||||
friend struct Encoder<Ok<Args...>>; | ||||||||
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 commentThe reason will be displayed to describe this comment to others. Learn more. We know that
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. Also, keeping this would enable constructing like so: return fine::Ok<std::string> {}; Which I don't think is wanted. |
||||||||
|
||||||||
explicit Ok(Args... items) : m_items{std::move(items)...} {} | ||||||||
|
||||||||
template <typename... UArgs> | ||||||||
Ok(const Ok<UArgs...> &other) : m_items(other.items()) {} | ||||||||
|
||||||||
template <typename... UArgs> | ||||||||
Ok(Ok<UArgs...> &&other) : m_items(std::move(other).items()) {} | ||||||||
|
||||||||
std::tuple<Args...> items; | ||||||||
const Items &items() const & noexcept { return m_items; } | ||||||||
|
||||||||
Items &items() & noexcept { return m_items; } | ||||||||
|
||||||||
Items &&items() && noexcept { return std::move(m_items); } | ||||||||
|
||||||||
private: | ||||||||
std::tuple<Args...> m_items; | ||||||||
brodeuralexis marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||
}; | ||||||||
|
||||||||
// Represents a `:error` tagged tuple, useful as a NIF result. | ||||||||
template <typename... Args> class Error { | ||||||||
public: | ||||||||
Error(const Args &...items) : items(items...) {} | ||||||||
using Items = std::tuple<Args...>; | ||||||||
|
||||||||
private: | ||||||||
friend struct Encoder<Error<Args...>>; | ||||||||
template <typename = std::enable_if_t<std::is_default_constructible_v<Items>>> | ||||||||
Error() : m_items() {} | ||||||||
brodeuralexis marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||
|
||||||||
explicit Error(Args... items) : m_items{std::move(items)...} {} | ||||||||
|
||||||||
template <typename... UArgs> | ||||||||
Error(const Error<UArgs...> &other) : m_items(other.items()) {} | ||||||||
|
||||||||
template <typename... UArgs> | ||||||||
Error(Error<UArgs...> &&other) : m_items(std::move(other).items()) {} | ||||||||
|
||||||||
std::tuple<Args...> items; | ||||||||
const Items &items() const & noexcept { return m_items; } | ||||||||
|
||||||||
Items &items() & noexcept { return m_items; } | ||||||||
|
||||||||
Items &&items() && noexcept { return std::move(m_items); } | ||||||||
|
||||||||
private: | ||||||||
std::tuple<Args...> m_items; | ||||||||
}; | ||||||||
|
||||||||
namespace __private__ { | ||||||||
|
@@ -895,7 +925,7 @@ template <typename... Args> struct Encoder<Ok<Args...>> { | |||||||
auto tag = __private__::atoms::ok; | ||||||||
|
||||||||
if constexpr (sizeof...(Args) > 0) { | ||||||||
return fine::encode(env, std::tuple_cat(std::tuple(tag), ok.items)); | ||||||||
return fine::encode(env, std::tuple_cat(std::tuple(tag), ok.items())); | ||||||||
} else { | ||||||||
return fine::encode(env, tag); | ||||||||
} | ||||||||
|
@@ -907,7 +937,7 @@ template <typename... Args> struct Encoder<Error<Args...>> { | |||||||
auto tag = __private__::atoms::error; | ||||||||
|
||||||||
if constexpr (sizeof...(Args) > 0) { | ||||||||
return fine::encode(env, std::tuple_cat(std::tuple(tag), error.items)); | ||||||||
return fine::encode(env, std::tuple_cat(std::tuple(tag), error.items())); | ||||||||
} else { | ||||||||
return fine::encode(env, tag); | ||||||||
} | ||||||||
|
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!