Skip to content

Recursive buildSequence does not compile #421

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Temp1ar opened this issue Jul 5, 2018 · 2 comments
Closed

Recursive buildSequence does not compile #421

Temp1ar opened this issue Jul 5, 2018 · 2 comments

Comments

@Temp1ar
Copy link

Temp1ar commented Jul 5, 2018

class Node {
    val children : List<Node> = emptyList()
}

fun Node.subtree() = kotlin.coroutines.experimental.buildSequence {
    val me = this@subtree
    yield(me)
    //me.children.forEach { yieldAll(it.subtree()) }
    for(child in me.children) {
        for (ancestor in child.subtree())
            yield(ancestor)
    }
}

The commented code fails to compile with "No resolved code for yieldAll" generator exception
Uncommented code with recursive type dependency error.
Can i somehow get it working?
#7 issue is probably related.

@Temp1ar
Copy link
Author

Temp1ar commented Jul 5, 2018

As a workaround i used approach from stackoverflow using lazyPlus:

fun Node.subtree2(): Sequence<Node> {
    return sequenceOf(this) lazyPlus {
        children.asSequence().flatMap { it.subtree2() }
    }
}

@elizarov
Copy link
Contributor

elizarov commented Jul 10, 2018

Hm... works for me in the laters version of Kotlin. Anyway, if you are running it recursive type problem, you should just specify type explicitly. In this case you can either use buildSequence<Node> or add type annotation to the function fun Node.subtree(): Sequence<Node>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants