How do I define a sequence that enumerates all anc...
# getting-started
m
How do I define a sequence that enumerates all ancestors of a node in a tree (nodes have multiple parents)? Code in 🧵
My try is the below, but it produces a `Suspension functions can be called only within coroutine body`error on the `yieldAll`call.
Copy code
fun allElementAncestors() = sequence {
        fun visitParents(visited: Element) {
            yieldAll(visited.elementParents)
            for (parent in visited.elementParents) {
                visitParents(parent.element)
            }
        }

        visitParents(this@Element)
    }
d
Make visit parents a suspend function
m
Tried that too, but this yields an `Restricted suspending functions can only invoke member or extension suspending functions on their restricted coroutine scope`error on `yieldAll`and
visitParents(this@Element)
d
You have to annotate it with the restricts suspension annotation. The same one yield all has
👀 1