Daniele B
06/16/2021, 9:16 AMsuspend fun getMyObject() : MyObject {
...
return myObject
}
val myApp = myCallback { myObject ->
/* code using myObject */
}
how should I write the function myCallback
?wbertan
06/16/2021, 9:27 AMblock
but also return a value for myApp
?
For example (Using String
as the MyObject
):
suspend fun getMyObject(): String {
return "something"
}
suspend fun myCallback(block: suspend (String) -> String): String {
return block(getMyObject())
}
val myApp = myCallback { myObject ->
myObject
}
This makes your block
responsible to return the value to assign to myApp
.
If you want directly the value from getMyObject
but ALSO execute the block
with the value, can be something like this:
suspend fun myCallback(block: suspend (String) -> Unit): String {
return getMyObject().also { block(it) }
}
val myApp = myCallback { myObject ->
// do something with myObject
}
Daniele B
06/16/2021, 9:30 AMmyCallback
returning a valueDKMPViewModel.getWebInstance()
is a suspend functionSuspend function 'myComposeApp' should be called only from a coroutine or another suspend function
streetsofboston
06/16/2021, 11:33 AMrunBlocking { ...}
or declare your suspend fun main()
as suspend.CLOVIS
06/16/2021, 4:45 PMDaniele B
06/16/2021, 9:52 PM