Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 8 additions & 8 deletions include/zeus/expected.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2441,7 +2441,7 @@ class expected<void, E>
constexpr auto and_then(F &&f) &
{
using U = expected_detail::remove_cvref_t<std::invoke_result_t<F>>;
static_assert(expected_detail::is_specialization_v<U, expected>, "U (return type of F) must be specialization of expected");
static_assert(expected_detail::is_specialization_v<U, zeus::expected>, "U (return type of F) must be specialization of expected");
static_assert(std::is_same_v<typename U::error_type, E>, "The error type must be the same after calling the F");

if (has_value())
Expand All @@ -2453,7 +2453,7 @@ class expected<void, E>
constexpr auto and_then(F &&f) const &
{
using U = expected_detail::remove_cvref_t<std::invoke_result_t<F>>;
static_assert(expected_detail::is_specialization_v<U, expected>, "U (return type of F) must be specialization of expected");
static_assert(expected_detail::is_specialization_v<U, zeus::expected>, "U (return type of F) must be specialization of expected");
static_assert(std::is_same_v<typename U::error_type, E>, "The error type must be the same after calling the F");

if (has_value())
Expand All @@ -2465,7 +2465,7 @@ class expected<void, E>
constexpr auto and_then(F &&f) &&
{
using U = expected_detail::remove_cvref_t<std::invoke_result_t<F>>;
static_assert(expected_detail::is_specialization_v<U, expected>, "U (return type of F) must be specialization of expected");
static_assert(expected_detail::is_specialization_v<U, zeus::expected>, "U (return type of F) must be specialization of expected");
static_assert(std::is_same_v<typename U::error_type, E>, "The error type must be the same after calling the F");

if (has_value())
Expand All @@ -2477,7 +2477,7 @@ class expected<void, E>
constexpr auto and_then(F &&f) const &&
{
using U = expected_detail::remove_cvref_t<std::invoke_result_t<F>>;
static_assert(expected_detail::is_specialization_v<U, expected>, "U (return type of F) must be specialization of expected");
static_assert(expected_detail::is_specialization_v<U, zeus::expected>, "U (return type of F) must be specialization of expected");
static_assert(std::is_same_v<typename U::error_type, E>, "The error type must be the same after calling the F");

if (has_value())
Expand All @@ -2490,7 +2490,7 @@ class expected<void, E>
constexpr auto or_else(F &&f) &
{
using G = expected_detail::remove_cvref_t<std::invoke_result_t<F, decltype(error())>>;
static_assert(expected_detail::is_specialization_v<G, expected>, "G (return type of F) must be specialization of expected");
static_assert(expected_detail::is_specialization_v<G, zeus::expected>, "G (return type of F) must be specialization of expected");
static_assert(std::is_same_v<typename G::value_type, T>, "The value type must be the same after calling the F");

if (has_value())
Expand All @@ -2502,7 +2502,7 @@ class expected<void, E>
constexpr auto or_else(F &&f) const &
{
using G = expected_detail::remove_cvref_t<std::invoke_result_t<F, decltype(error())>>;
static_assert(expected_detail::is_specialization_v<G, expected>, "G (return type of F) must be specialization of expected");
static_assert(expected_detail::is_specialization_v<G, zeus::expected>, "G (return type of F) must be specialization of expected");
static_assert(std::is_same_v<typename G::value_type, T>, "The value type must be the same after calling the F");

if (has_value())
Expand All @@ -2514,7 +2514,7 @@ class expected<void, E>
constexpr auto or_else(F &&f) &&
{
using G = expected_detail::remove_cvref_t<std::invoke_result_t<F, decltype(std::move(error()))>>;
static_assert(expected_detail::is_specialization_v<G, expected>, "G (return type of F) must be specialization of expected");
static_assert(expected_detail::is_specialization_v<G, zeus::expected>, "G (return type of F) must be specialization of expected");
static_assert(std::is_same_v<typename G::value_type, T>, "The value type must be the same after calling the F");

if (has_value())
Expand All @@ -2526,7 +2526,7 @@ class expected<void, E>
constexpr auto or_else(F &&f) const &&
{
using G = expected_detail::remove_cvref_t<std::invoke_result_t<F, decltype(std::move(error()))>>;
static_assert(expected_detail::is_specialization_v<G, expected>, "G (return type of F) must be specialization of expected");
static_assert(expected_detail::is_specialization_v<G, zeus::expected>, "G (return type of F) must be specialization of expected");
static_assert(std::is_same_v<typename G::value_type, T>, "The value type must be the same after calling the F");

if (has_value())
Expand Down
57 changes: 57 additions & 0 deletions tests/test_expected/monadic_tests.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include <string>
#include <type_traits>

#include <catch2/catch_all.hpp>

Expand Down Expand Up @@ -30,3 +31,59 @@ TEST_CASE("and_then()", "[monadic]")
static_assert(std::is_same_v<std::remove_cv_t<decltype(newVal)>, Expected2>);
}
}

TEST_CASE("void-T and_then()", "[monadic, void-T]")
{
using T = std::string;
using Expected = expected<void, T>;

{ // non-void
using Expected2 = expected<double, T>;

Expected e {};

auto const newVal = e.and_then([]() { return Expected2 {}; });

static_assert(std::is_same_v<std::remove_cv_t<decltype(newVal)>, Expected2>);
}
{ // void
using Expected2 = expected<void, T>;

Expected e {};

auto const newVal = e.and_then([]() -> Expected2 { return Expected2 {}; });

static_assert(std::is_same_v<std::remove_cv_t<decltype(newVal)>, Expected2>);
}
}

TEST_CASE("or_else()", "[monadic]")
{
using T = std::string;
using Expected = expected<T, int>;

{ // non-void
using Expected2 = expected<T, double>;

Expected e {};

auto const newVal = e.or_else([](auto&&) { return Expected2 {}; });

static_assert(std::is_same_v<std::remove_cv_t<decltype(newVal)>, Expected2>);
}
}

TEST_CASE("void-T or_else()", "[monadic, void-T]")
{
using Expected = expected<void, int>;

{ // non-void
using Expected2 = expected<void, double>;

Expected e {};

auto const newVal = e.or_else([](auto&&) { return Expected2 {}; });

static_assert(std::is_same_v<std::remove_cv_t<decltype(newVal)>, Expected2>);
}
}