https://kotlinlang.org logo
Title
a

Animesh Sahu

11/12/2019, 4:10 PM
somebody know some inbuilt function similar to apply() which can execute suspendable block of code?
s

Seri

11/12/2019, 4:14 PM
apply()
has the
inline
property, which allows it to copy the suspending context of its caller
i.e. if you’re already calling it from a suspending block of code, you’re good
inline fun  T.apply(block: T.() -> Unit): T
a

Animesh Sahu

11/12/2019, 4:16 PM
@Seri It does not work, in my case if you can correct some kindof code:
kotlin
private suspend fun createUserSession(session: WebSocketServerSession, block: suspend UserSessionBuilder.() -> Unit): UserSession {
		return UserSessionBuilder(session, server).apply(block).create()
	}
here apply(block) give compiler error that type interference failed
Type inference failed: required: UserSessionBuilder.()->Unit found: suspend UserSessionBuilder.()->Unit
t

tseisel

11/12/2019, 4:20 PM
Suspend lambdas are not compatible with regular lambdas. You cannot pass it directly as a parameter of
apply
, but should instead write :
return UserSessionBuilder(session, server).apply { block() }.create()
a

Animesh Sahu

11/12/2019, 4:21 PM
Exactly, thanks @tseisel
s

Seri

11/12/2019, 4:23 PM
Ah, thanks