I’m using suspended iterators to generate state machine code for me. So far i only called stuff within class hierarchy and everything worked fine. I now want to move some code to other objects and just don’t understand when (and why) kotlin allows or wont allow me to call suspend funs of other objects. Example code:
// kotlin_version = '1.2.31'
import kotlin.coroutines.experimental.*
class SBTest{
suspend fun SequenceBuilder<Int>.myCommand(data: Int): Boolean{
yield(data)
return true
}
class someWrapper(val sb: SBTest){
suspend fun SequenceBuilder<Int>.failingFun(){
sb.myCommand(1) // fails?
this@failingFun.myCommand(1) // fails as expected
sb.run {
myCommand(1) // works
this@failingFun.myCommand(1) // works, now?! why?
this.myCommand(1) // fails with different error
}
}
}
suspend fun SequenceBuilder<Int>.workingFun(){
myCommand(1) // works
this.myCommand(1) // works
this@workingFun.myCommand(1) // works
}
fun i() = buildIterator {
workingFun() // works
someWrapper(this@SBTest).failingFun(); // fails
someWrapper(this@SBTest).run {
failingFun() // works
}
}
}
fun main(args: Array<String>) {
SBTest().i().forEach {
println("Hello, world!")
}
}
I’d expect all but one calls to myCommand to work. Can someone explain why they fail? Is that intended, am I doing something wrong, or am I running into limitations of an unfinished feature? Might there be any better way to get “suspend after every step” state machine that also yields data and has individual functions returning values?
Thanks for you help.