我在Kotlin中遇到有关generic
类型的一些问题。本来,我想用值和调用函数来调用函数。所以我在下面写下,但是该值只允许我传递类似string / Int这样的类型。我想在回调函数中将函数作为参数传递。
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
}
我尝试做类似的事情
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
}
但是显示语法错误。有人可以帮忙吗?
语法错误
Error show in this place only RYEasyDialog means ClassA
有效!!现在
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更新 Vararg问题
我正在做高级操作。我只是将其发布在完整的代码中。 如果我不使用vararg,则
EasyDialog<Observable<Any>,LiveData<Boolean>>(ctx, true,viewModel!!::callGetTeammateListAPITEST,recallFunctionWithFunction = viewModel!!::callMultipleAPI).createNativeNetworkDialog2()
不强制转换as ((Observable<Any>)->LiveData<Boolean>)
。但是callMuipleApi接收到vararg参数,我必须将其强制转换为((Observable)-> LiveData),但是我没有显示任何错误,但函数callmutipleapi和callgetteammateslist没有被调用。
有人可以帮忙吗?
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!!
}
片段
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()) }
}
答案 0 :(得分:0)
最后一个构造方法参数是
(() -> T) -> Unit
根据您为第三个构造函数参数传递的内容,推断T
为Observable<Any>?
。所以它期待一个
(() -> Observable<Any>?) -> Unit
但是testRefOutside
是
Observable<Any>? -> Unit
我猜您可能不是要通过将函数的输入设为另一个函数来定义三阶函数。