https://kotlinlang.org logo
#announcements
Title
# announcements
a

Antanas A.

06/21/2019, 8:18 AM
Hi, is there a way to write anonymous suspend function? ``` val func = { // <- here I cannot call suspend functions } ``
t

tseisel

06/21/2019, 8:24 AM
There's a special syntax to define a
suspend
lambda :
Copy code
val func = suspend {
    delay(100)
}
s

spand

06/21/2019, 8:26 AM
Its actually just a normal inline function
a

Antanas A.

06/21/2019, 8:28 AM
it will work with no argument
but actually I need an anonymous functions with parameters
Copy code
val func = suspend { from: Int ->

            }
don't work
s

spand

06/21/2019, 8:29 AM
Then put the proper type on the func:
val func : suspend (Int) -> Unit = {}
a

Antanas A.

06/21/2019, 8:31 AM
so actually to write anonymous suspend function I need to assign it to some variable with explicitly defined type?
s

spand

06/21/2019, 8:35 AM
Pretty sure suspend has to be inferred for lambdas
d

Dico

06/21/2019, 8:35 AM
Yeah sadly this is not yet supported.
You can declare the type of the variable explicitly as a workaround
a

Antanas A.

06/21/2019, 8:58 AM
ok, thanks
s

spand

06/21/2019, 9:05 AM
If you want then you can still make your own
suspend
function like the stdlib one that takes one, two, three args or whatever you need
d

Dico

06/21/2019, 6:41 PM
But you would need to name it differently
2 Views