v0ldem0rt
08/05/2019, 1:18 PMsuspend operator fun <T> invoke(block: suspend () -> T): T {
// ...
}
to work. Does invoke
work with suspend
keyword?Eric Martori
08/05/2019, 1:28 PM@Test
fun q() {
val a = A()
runBlocking {
launch {
val x = a {
2
}
assertEquals(2, x)
}.join()
}
}
class A {
suspend operator fun <Q> invoke(block: suspend () -> Q) : Q{
delay(2)
return block()
}
}
wbertan
08/05/2019, 1:28 PMclass AsasSuspended: suspend (suspend () -> Int) -> Int {
override suspend fun invoke(p1: suspend () -> Int): Int {
TODO("not implemented")
}
}
But I'm getting a suspend function type is not allowed as supertypes
.Melih Aksoy
08/05/2019, 1:30 PMsuspend
is just a modifier, in this case stating you can’t invoke T
outside of coroutines. I tried an example and seems working fine.
What seems to be problem for you ?
P.S.: Playground example -> https://play.kotlinlang.org/#eyJ2ZXJzaW9uIjoiMS4zLjMxIiwiY29kZSI6ImltcG9ydCBrb3RsaW54LmNvcm91dGluZXMuKlxuaW1wb3J0IGtvdGxpbi5zeXN0ZW0uKlxuXG5mdW4gbWFpbigpIHtcbiAgICBydW5CbG9ja2luZyB7XG4gICAgXHR2YWwgeCA9IFN0cmluZyB7XG4gICAgICAgXHRcdHByaW50bG4oXCJJJ20gaW52b2tlZCAhXCIpXG5cdCAgICB9XG4gICAgICAgIFxuICAgICAgICBwcmludGxuKHgpXG4gICAgfVxufVxuXG4gc3VzcGVuZCBvcGVyYXRvciBmdW4gPFQ+IFQuaW52b2tlKGJsb2NrOiBzdXNwZW5kICgpIC0+IFQpOiBUIHtcbiAgICBydW5CbG9ja2luZyB7XG4gICAgICAgIGJsb2NrKClcbiAgICB9XG4gICAgXG5cdHJldHVybiB0aGlzXG4gfSIsInBsYXRmb3JtIjoiamF2YSIsImFyZ3MiOiIifQ==v0ldem0rt
08/05/2019, 1:30 PMZach Klippenstein (he/him) [MOD]
08/05/2019, 4:24 PM