I've got a java method visitChildrenRecursively(Node root, NodeVisitor p) that allows me to iterate over elements of a tree-like structure by providing a SAM visitor.
I'd like to convert this approach to a Sequence-based iteration.
However, this code...
buildSequence {
visitChildrenRecursively(root, NodeVisitor {
yield(it)
})
}
...fails compilation with error Suspension functions can be called only within coroutine body at line yield(it)
The Kotlin coroutines Informal description suggests my visitChildrenRecursively could be converted to a suspending function by wrapping it inside a suspendCoroutine block.
Would that be the correct approach, or is this even possible without resorting to a produce approach from kotlinx as shown in the kotlinx coroutines guide ?
I also tried section "Asynchronous sequences" in the Informal description, with no luck.