Reputation: 547
I'm making a listview (android) what call function when click.
and I want to get function is async or sync
.
to block when it is async.
and even i want to know how attach async mark to kotlin lambda expression
.
class FunctionCaller_Content(text: List<String>,
val function: List< /*suspend? @async? */
( () -> Unit )?
>? = null)
/* I want both of async, sync function. */
{
fun isAsnyc(order: Int): Boolean
= // how to get this lambda expression{function?.get(order)} is async?
fun call(callerActivity: Activity, order: Int) {
val fun = function?.get(order)
fun()
if(isAsync(fun))
/* block click for async func */
}
}
and usage.
FunctionCaller_Content( listOf("Click to Toast1", "Click to Nothing"),
listOf(
{
Toast.makeText(this, "clicked", Toast.LENGTH_SHORT)
},
{
/*if async lambda expression, how can i do?*/
} )
Upvotes: 4
Views: 3033
Reputation: 170919
You can have List<suspend () -> Unit>
, but you can't have both suspend and non-suspend functions in the same list except by using List<Any>
. I'd suggest using two separate lists instead. Another solution is to use "algebraic data type":
sealed class SyncOrAsync // can add methods here
class Sync(val f: () -> Unit) : SyncOrAsync
class Async(val f: suspend () -> Unit) : SyncOrAsync
class FunctionCaller_Content(text: List<String>,
val function: List<SyncOrAsync>? = null)
{
fun call(callerActivity: Activity, order: Int) {
val fun = function?.get(order)
if(fun is Async)
/* block click for async func */
}
}
FunctionCaller_Content(
listOf("Click to Toast1", "Click to Nothing"),
listOf(Sync {
Toast.makeText(this, "clicked", Toast.LENGTH_SHORT)
},
Async {
// your async code
})
But if you are just going to block anyway, I would just use List<() -> Unit>
and
listOf({
Toast.makeText(this, "clicked", Toast.LENGTH_SHORT)
},
{
runBlocking {
// your async code
}
})
Upvotes: 5