To: J3 J3/24-118 From: Van Snyder Subject: Coroutines and iterators Date: 2024-March-04 When conditional expressions were proposed in 04-192, part of the proposal was a mechanism to compute whether an actual argument is present. Conditional expressions were rejected, leaving no means to address the "combinatorial explosion" problem of multiple optional arguments. We ended up with a kludge in which a disassociated pointer or unallocated allocatable actual argument that corresponds to an optional nonpointer nonallocatable argument is considered to be absent. We now have the more elegant and more broadly useful mechanism of conditional expressions. Had we at first implemented that solution, we wouldn't have the kludge, which we now need to support indefinetly, until we decide to print it in small type, and then delete it, in a few decades or so. It has come to my attention that the possibility of a kludge to fake iterators using templates has been developed. Iterators are related to coroutines in the same way that functions are related to subroutines. Fortran has both funtions and subroutines. Both are useful in their own right. Coroutines are useful to allow to organize procedures that need access to "user code," such as mathematical software to evaluate integrals, solve differential equations, find zeroes of nonlinear functions, optimization, ..., and in other areas as well, using "reverse communication." Decades ago, others observed the utility of coroutines in connection with parallel programming. Ada providers and users observed that the "task" system of parallel programming implied unavoidable overhead, so a new mechanism called a "protected variable" was introduced. Ada protected variables are actually coroutines. Alan Burns and Andy Wellings describe this in "Concurrent and Real-Time Programming in Ada." At least 48 other languages support coroutines, and at least ten support iterators. If a kludge is used to fake iterators, and coroutines are rejected, Fortran will either have only iterators for a time when coroutines are equally if not more valuable, or will eventually be stuck with two mechanisms for them, one of which will be a kludge -- if the correct solution is eventually adopted. Coroutines were briefly part of the proposed work plan for 2004. When they were removed, I developed a TR proposal, which also was not adopted. For those who have forgotten the WG5 number for that proposal, it is the body of this proposal, as a PDF file.