Is it possible to somehow create a sequence from i...
# getting-started
n
Is it possible to somehow create a sequence from inside of a
DeepRecursiveFunction
?
Copy code
fun s() = sequence<Int> { 
    val f = DeepRecursiveFunction<Int, Unit> {
        yield(it) // Compile error: Restricted suspending functions can only invoke member or extension suspending functions on their restricted coroutine scope
        
        if (it < 1000) {
            callRecursive(it + 1)
        }
    }
    
    f(1)
}
e
no, it's there for a reason. SequenceScope and DeepRecursiveScope handle suspension differently and don't mix
1
even if you
@Suppress("ILLEGAL_RESTRICTED_SUSPENDING_FUNCTION_CALL")
to bypass the compiler error, it won't work
it doesn't even work with the same type of scope, e.g.
Copy code
sequence outer@{
    sequence inner@{
        this@outer.yield(Unit)
    }
}
it has to be the actual same scope under
@RestrictsSuspension
n
OK, thanks.
e
btw you don't need DeepRecursiveFunction here anyway. the sequence builder is transformed by the compiler into a state machine just other suspend functions
Copy code
private fun f(n: Int): Sequence<Int> = sequence {
    yield(n)

    if (n < 1000) {
        yieldAll(f(n + 1))
    }
}

fun s(): Sequence<Int> = f(1)
🙏 1
😮 1