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

Sam

11/26/2018, 7:58 PM
How do i create a class implementing a suspend block?
Copy code
class LambdaClass : suspend () -> Unit {

    override fun invoke() {
        // do something
    }

}
z

zpearce

11/26/2018, 8:46 PM
I don't think you can make a suspending function a supertype at the moment
s

Sam

11/26/2018, 8:47 PM
yeah looks like it
z

zpearce

11/26/2018, 8:47 PM
...would be handy
g

gildor

11/27/2018, 2:30 AM
What is your use case? You cannot have suspend constructor.
Just use builder suspend function instead
s

Sam

11/27/2018, 3:46 AM
Its not about suspend constructor, just a class to represent a suspend lambda block
with a regular lambda block, i can keep all logic including helper methods within the class and just create an instance of it at use site
I was wondering if that was possible with a suspend lambda
g

gildor

11/27/2018, 3:51 AM
Yeah, I see what you mean
b

bolot

12/02/2018, 9:30 PM
do you want something like
class MyLambda() { suspend operator fun invoke() {} }
?
g

gildor

12/03/2018, 1:59 AM
This is not direct relplacement, you cannot use such class as lambda
s

Sam

12/03/2018, 8:11 PM
yeah, that doesn't do it
5 Views