trompeloeil icon indicating copy to clipboard operation
trompeloeil copied to clipboard

Make `CO_YIELD` to handle heterogeneous values

Open chghehe opened this issue 7 months ago • 2 comments

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.

chghehe avatar Apr 18 '25 14:04 chghehe

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.

rollbear avatar Apr 21 '25 08:04 rollbear

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!";
}

chghehe avatar Apr 21 '25 09:04 chghehe