https://kotlinlang.org logo
#coroutines
Title
# coroutines
v

v0ldem0rt

08/05/2019, 1:18 PM
I can't seem to get
Copy code
suspend operator fun <T> invoke(block: suspend () -> T): T {
        // ...
    }
to work. Does
invoke
work with
suspend
keyword?
e

Eric Martori

08/05/2019, 1:28 PM
What errors are you getting? I could do it without problems:
Copy code
@Test
    fun q() {
        val a = A()
        runBlocking {
            launch {
                val x = a {
                    2
                }
                assertEquals(2, x)
            }.join()
        }
    }
Copy code
class A {
    suspend operator fun <Q> invoke(block: suspend () -> Q) : Q{
        delay(2)
        return block()
    }
}
w

wbertan

08/05/2019, 1:28 PM
Well, something like this:
Copy code
class 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
.
v

v0ldem0rt

08/05/2019, 1:30 PM
Ok it's IntelliJ
IntelliJ is showing squigglies 😠
it compiles fine
message has been deleted
z

Zach Klippenstein (he/him) [MOD]

08/05/2019, 4:24 PM
2 Views