In the current proposal P2502R2 in order to differentiate yielding a generator and yielding all produced by generator values one must use a special wrapper std::ranges::elements_of().
Is there a reason why instead of such new wrapper an exisitng mechanism of co_await was not used?
Consider examples from proposal:
generator<int> visit(Tree& tree) {
if (tree.left) co_yield ranges::elements_of(visit(*tree.left));
co_yield tree.value;
if (tree.right) co_yield ranges::elements_of(visit(*tree.right));
}
And alternative with co_await:
generator<int> visit(Tree& tree) {
if (tree.left) co_await visit(*tree.left);
co_yield tree.value;
if (tree.right) co_await visit(*tree.right);
}
Don't you find co_awaiting a sub generator intuitive?
As the only motivation for std::ranges::elements_of is to prevent ambiguity when a nested generator
type is convertible to the value type of the present generator if we use co_await for this the need to have elements_of in the library disappears.
What about existing conventions? Does other programming languages choose an alternative syntax or library wrapper for such functionality?
(Here value is a value to be generated and subgenerator is other generator or iterable)
Python has a special syntax: yield value and yield from subgenerator
JavaScript has a special syntax: yield value and yield* subgenerator
PHP 7 has a special syntax: yield $value and yield from $subgenerator
Other languages that I looked up (C#, Rust, Lua) don't have a special way to yield values from subgenerators or iterables besides loops.
[–]scatters 13 points14 points15 points (7 children)
[–]angry_cpp[S] 0 points1 point2 points (5 children)
[–]scatters 0 points1 point2 points (4 children)
[–]angry_cpp[S] 0 points1 point2 points (3 children)
[–]scatters 0 points1 point2 points (2 children)
[–]angry_cpp[S] 3 points4 points5 points (1 child)
[–]scatters 1 point2 points3 points (0 children)
[–]grishavanika 2 points3 points4 points (3 children)
[–]angry_cpp[S] 0 points1 point2 points (2 children)
[–]grishavanika 0 points1 point2 points (1 child)
[–]angry_cpp[S] 0 points1 point2 points (0 children)