[boost.asynch] another generator query
Hi Klemens, I have another question regarding the generator. I think the initial examples in the dcs for `generator` are too simplistic, because the caller seems to already know how many times it is allowed to co_await on the Awaitable. I tried it with a loop, where I use the explicit conversion to bool to check when to break the iteration: ``` async::generatorstd::string worker(int limit) { for (int i = 0; i < limit; ++i) { co_yield std::format("--{}--", i); } } async::main co_main(int argc, char* argv[]) { try { auto w = worker(3); while (w) { std::cout << co_await w << std::endl; } } catch (std::exception const& e) { std::cout << "ERR: " << e.what() << std::endl; } } ``` But I am surprised that this construct ends with throwing an exception. Am I doing something wrong? Regards, &rzej;
On Sat, Aug 19, 2023 at 4:41 AM Andrzej Krzemienski via Boost
Hi Klemens, I have another question regarding the generator. I think the initial examples in the dcs for `generator` are too simplistic, because the caller seems to already know how many times it is allowed to co_await on the Awaitable. I tried it with a loop, where I use the explicit conversion to bool to check when to break the iteration:
``` async::generatorstd::string worker(int limit) { for (int i = 0; i < limit; ++i) { co_yield std::format("--{}--", i); } }
This is missing a co_return at the end. Because the generators are async I can't look ahead like std::generator. So this should be async::generatorstd::string worker(int limit) { for (int i = 0; i < limit; ++i) { if (i == (limit - 1)) co_return std::format("--{}--", i); co_yield std::format("--{}--", i); } } Or you return and empty string and handle it on the consumer side as an EOI.
async::main co_main(int argc, char* argv[]) { try { auto w = worker(3); while (w) { std::cout << co_await w << std::endl; } } catch (std::exception const& e) { std::cout << "ERR: " << e.what() << std::endl; } } ```
But I am surprised that this construct ends with throwing an exception. Am I doing something wrong?
Regards, &rzej;
_______________________________________________ Unsubscribe & other changes: http://lists.boost.org/mailman/listinfo.cgi/boost
sob., 19 sie 2023 o 03:37 Klemens Morgenstern < klemensdavidmorgenstern@gmail.com> napisał(a):
On Sat, Aug 19, 2023 at 4:41 AM Andrzej Krzemienski via Boost
wrote: Hi Klemens, I have another question regarding the generator. I think the initial examples in the dcs for `generator` are too
simplistic,
because the caller seems to already know how many times it is allowed to co_await on the Awaitable. I tried it with a loop, where I use the explicit conversion to bool to check when to break the iteration:
``` async::generatorstd::string worker(int limit) { for (int i = 0; i < limit; ++i) { co_yield std::format("--{}--", i); } }
This is missing a co_return at the end. Because the generators are async I can't look ahead like std::generator. So this should be
async::generatorstd::string worker(int limit) { for (int i = 0; i < limit; ++i) { if (i == (limit - 1)) co_return std::format("--{}--", i); co_yield std::format("--{}--", i); } }
Ok, so I am hearing that "async::generator is async and std::generator is sync". I do not know what that means exactly. I guess I do not know what it means for a generator to be async. Second, I report a concern that Boost.async reuses a name 'generator' that is already used in STD, in a similar context, but with a different meaning: different enough that the intuition from std::generator cannot be applied to async::generator. Third, I recommend devoting more space to `generator` in the documentation: (1) the initial example should be more real-life. For instance, demonstrating how `operator bool` is used correctly. (2) the tutorial section should have one example containing `generator`. Currently there is none and this seems to indicate that generator is a second-class type in this library. You mentioned the state machine in another email. Maybe this would be a good fit. It looks like I am just dumping requests. I am unable to offer help with it, as I genuinely cannot build any intuition around `async::generator`. Regards, &rzej;
Or you return and empty string and handle it on the consumer side as an EOI.
async::main co_main(int argc, char* argv[]) { try { auto w = worker(3); while (w) { std::cout << co_await w << std::endl; } } catch (std::exception const& e) { std::cout << "ERR: " << e.what() << std::endl; } } ```
But I am surprised that this construct ends with throwing an exception. Am I doing something wrong?
Regards, &rzej;
_______________________________________________ Unsubscribe & other changes:
participants (2)
-
Andrzej Krzemienski
-
Klemens Morgenstern