Anonymous suspending functions

I have a class which takes a function as parameter, which will then only be called from coroutines. But since the compiler doesn’t know that, I can’t call delay inside of it.

The docs state “anonymous suspending functions are currently not supported”. Are they planned?

nevermind I just found out that the launch function uses an anonymous suspend function, are the docs wrong?

Doc is right. Anonymous suspending function is suspend fun() { ... } which is not supported. However, suspending functional types like suspend () -> Unit and suspending lambdas are supported, which enables functions like launch which can take suspending lambda like launch { ... }

4 Likes

There is a use case for suspend anonymous function: without it, it’s impossible to write a lambda function with infinite loop inside it without warnings; see https://youtrack.jetbrains.net/issue/KT-22404#focus=streamItem-27-2688870-0-0.