i stuck in some problems about the generic
type in kotlin. Originally,i want to call the function with the value and the recall-function. So i write that below, but the value only allows me pass a type like string/Int somethings like that. I want to pass a function as parameter in the recall function.
ClassA
ClassA<T>{
var canCancel: Boolean = true
var recallFunction: (() -> T)? = null
var recallFunctionWithValue: ((value: T) -> Unit)? = null
var context: Context? = null
var value: T? = null
var functionPara :( ()->T)? = null
var recallFunctionWithFunction:((()->T) -> Unit)? = null
constructor(context: Context?, canCancel: Boolean, value: T, recallFunctionWithValue: (value: T) -> Unit) {
this.context = context
this.canCancel = canCancel
this.value = value
this.recallFunctionWithValue = recallFunctionWithValue
}
}
ClassB
ClassA(ctx, true,viewModel!!::testRefInside,viewModel!!::testRefOutside).createNativeNetworkDialog()
ViewModel
fun testRefOutside(test: Observable<Any>? ){
}
fun testRefInside():Observable<Any>?{
return null
}
i try to do something like that
var canCancel: Boolean = true
var recallFunction: (() -> T)? = null
var recallFunctionWithValue: ((value: T) -> Unit)? = null
var context: Context? = null
var value: T? = null
var functionPara :( ()->T)? = null
var recallFunctionWithFunction:((()->T) -> Unit)? = null
constructor(context: Context?, canCancel: Boolean, value: () -> T, recallFunctionWithValue: (value: ()->T) -> Unit) {
this.context = context
this.canCancel = canCancel
this.functionPara = value
this.recallFunctionWithFunction = recallFunctionWithValue
}
but it shows the syntax error. Can someone help?
Syntax Error
Error show in this place only RYEasyDialog means ClassA
It works!! now
var recallFunctionWithFunction:((value: T) -> Unit)? = null
constructor(context: Context?, canCancel: Boolean, value: () -> T, recallFunctionWithValue: (value: T) -> Unit) {
this.context = context
this.canCancel = canCancel
this.functionPara = value
this.recallFunctionWithFunction = recallFunctionWithValue
8/6 Updated Vararg problems
I am doing something in advanced. I just post it in completed code. If i don't use vararg, the
EasyDialog<Observable<Any>,LiveData<Boolean>>(ctx, true,viewModel!!::callGetTeammateListAPITEST,recallFunctionWithFunction = viewModel!!::callMultipleAPI).createNativeNetworkDialog2()
without casting as ((Observable<Any>)->LiveData<Boolean>)
.but the callMuipleApi receives vararg parameter, i have to cast it into as ((Observable)->LiveData) but i it doesn't show any error but the function callmutipleapi and callgetteammateslist in not called. can anyone help?
ViewModel
fun callMultipleAPI( vararg observable: Observable<Any>):LiveData<Boolean>{
if(progressDialogData==null){
progressDialogData= MutableLiveData()
}
progressDialogData?.setValue(true)
repo.callMultipleAPI<Any>(*observable, observer = RYEasyObserver(object : RyObserverSingleStatusListener<List<Any>> {
override fun onNext(g: List<Any>) {
Log.d("test",g.toString())
for(model in g){
when(model){
is GetTeammateListModel ->{setCallGetTeammateListResponse(model)}
is GetTeamListModel ->{setCallGetTeamListResponse(model)}
is GetMessageSumModel->{setCallGetMessageSumResponse(model)}
is GetCallModel->{setCallGetResponse(model)}
}
}
progressDialogData?.setValue(false)
}
override fun onError() {
progressDialogData?.setValue(false)
}
},true))
return progressDialogData!!
}
Fragment
EasyDialog<Observable<Any>,LiveData<Boolean>>(ctx, true,viewModel!!::callGetTeammateListAPITEST,recallFunctionWithFunction = viewModel!!::callMultipleAPI as ((Observable<Any>)->LiveData<Boolean>)).createNativeNetworkDialog2()
EasyDialog
class EasyDialog<T,V> {
var canCancel: Boolean = true
var recallFunction: (() -> T)? = null
var recallFunctionWithValue: ((value: T) -> Unit)? = null
var context: Context? = null
var value: T? = null
var functionPara :(()->T)? = null
var recallFunctionWithFunction:((T) -> V)? = null
constructor(
context: Context?, canCancel: Boolean, value: ()->T, recallFunctionWithFunction: (T) -> V ){
this.context = context
this.canCancel = canCancel
this.functionPara = value
this.recallFunctionWithFunction = recallFunctionWithFunction
}
fun createNativeNetworkDialog2() {
val builder = AlertDialog.Builder(context)
if (!canCancel) {
builder.setCancelable(false)
} else {
builder.setNegativeButton(context?.resources?.getString(R.string.gloda_cancel)) { dialogInterface, id -> }
}
builder.setMessage(context?.resources?.getString(R.string.global_network_error))
builder.setPositiveButton(context?.resources?.getString(R.string.global_retry)) { dialog, id ->
recallFunction?.invoke()
value?.let { it1 -> recallFunctionWithValue?.invoke(it1) }
//functionPara?.let { it2->recallFunctionWithFunction?.invoke(it2) }
functionPara?.let{ functionPara->{ recallFunctionWithFunction?.let {
recallFunctionWithFunction-> compose<T,V>(recallFunctionWithFunction ,functionPara).invoke()
}}}
}
val dialog = builder.create()
dialog.show()
}
fun <T, V> compose( f: (T) -> V, g:() -> T ): () -> V {
return { f(g()) }
}
The last constructor parameter is
(() -> T) -> Unit
Based on what you pass for the third constructor paramter, T
is inferred to be Observable<Any>?
. so it is expecting a
(() -> Observable<Any>?) -> Unit
but testRefOutside
is
Observable<Any>? -> Unit
I'm guessing you probably didn't mean to define a third-order function by making the input of your function another function.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.