mardi 9 juin 2020

kotlin reference function with vararg parameter and convert Array

I have asked the question based on this topic Base Question

So, i want to ask in advanced. With someone answered the question with Array and List

Class Test<T,V>{
   var functionPara :(()->T)? = null
   var recallFunctionWithFunction:( (Array<out T>) -> V)? = null

   constructor(value: ()->T, recallFunctionWithFunction:   (Array<out T>) -> V  ){
   this.functionPara = value
   this.recallFunctionWithFunction = recallFunctionWithFunction
}
inline fun <reified T, V> compose(crossinline f: (Array<out T>) -> V, vararg g: () -> T): () -> V {
val results = g.map { it() }
return { f(results.toTypedArray()) }
}

fun <T, V> compose(f: (List<out T>) -> V, vararg g: () -> T): () -> V {
val results = g.map { it() }
return { f(results) }
}
}
fun runCompose(){
compose(functionPara,recallFunctionWithFunction).invoke()
}

But i discovered that When i reference a function with vararg parameter like that

fun functionA(vararg :Observable<Any>):LiveData<Boolean>{
}
fun functionB():Observable<Any>{
}

When i do something like ::functionA the type of A will be Array<out Observable<Any>>->LiveData<Boolean> Therefore , when i do something like that

Test<Observable<Any>,LiveData<Boolean>>(::functionB,::functionA).runCompose()

Situation 1 If I use the compose function with accepting List type it will show type mismatch due to referencing ::functionA will return Array

Image1

Situation 2 If i use the compose function with accepting Array type it will show the error

Cannot use 'T' as reified type parameter. Use a class instead

Image2

In previous post , some answered me that convert the array to list. But how to convert reference function with vararg parameter with Originally Array<out to List <out ?? when i reference a function like ::function the type must be Array<out but i want to plug it into compose function. I must convert it. Can anyone help? i stuck in there long days. Hope someone can save me!!





Aucun commentaire:

Enregistrer un commentaire