trompeloeil
trompeloeil copied to clipboard
Make `CO_YIELD` to handle heterogeneous values
Coroutines may co_yield different types (even void).
Due to coroutine operators co_await, co_yield and co_return must be inlined in the function body, the simplest approach is to use tuples:
co_mock m;
REQUIRE_CALL(m, gen()).CO_YIELD(1, "hello", yield_void).CO_RETURN(90); // will yield 3 values and return 90
REQUIRE_CALL(m, gen()).CO_YIELD().CO_THROW(std::runtime_error("error")); // will yield one void value and throw
We can still use chaining approach (.CO_YIELD(10).CO_YIELD().CO_YIELD("hello")), but I cannot find any implementation of this except of creating template-based expression of expectations and then compile it (like in Boost.Spirit). This will require to reimplement library basics.
See also #347.
Wow. I had no idea that this was even allowed. Can you please show an example of a coroutine that behaves this way? I have some learning to do.
Coroutines might be custom and it is allowed, but there are no such standard coroutines.
[]() -> /* custom coroutine type */
{
co_yield 1;
co_yield;
co_yield "Hello, world!";
}