不完成类型推断就无法在以下候选者中进行选择

时间:2019-03-13 12:15:03

标签: kotlin kotlin-extension

我有以下三个辅助扩展:

fun <T, K : BaseViewHolder> BaseQuickAdapter<T, K>.onItemClick(
        onItemClick: (adapter: BaseQuickAdapter<*, *>, position: Int, item: T) -> Unit
): BaseQuickAdapter<T, K> {
    setOnItemClickListener { adapter, _, position ->
        onItemClick(adapter, position, data[position])
    }
    return this
}

fun <T, K : BaseViewHolder> BaseQuickAdapter<T, K>.onItemClick(
        onItemClick: (position: Int, item: T) -> Unit
): BaseQuickAdapter<T, K> =
        onItemClick { _, position, item -> onItemClick(position, item) }

fun <T, K : BaseViewHolder> BaseQuickAdapter<T, K>.onItemClick(
        onItemClick: (item: T) -> Unit
): BaseQuickAdapter<T, K> =
        onItemClick { _, item -> onItemClick(item) }

每个onItemClick函数所接受的参数要比前一个少

当我打电话时:

onItemClick { item ->  
}

一切正常,并调用了最后一个方法。

但是当我删除明确的item广告时,尝试使用it

onItemClick {
    it.doStuff()
}

我收到此错误:

Cannot choose among the following candidates without completing type inference:
public fun <T, K : BaseViewHolder> BaseQuickAdapter<T#1 (type parameter of humazed.github.com.kotlinandroidutils.simpleAdapter), KBaseViewHolder>.onItemClick(onItemClick: (adapter: BaseQuickAdapter<*, *>, position: Int, item: T#1) → Unit): BaseQuickAdapter<T#1, KBaseViewHolder> defined in humazed.github.com.kotlinandroidutils
public fun <T, K : BaseViewHolder> BaseQuickAdapter<T#1, KBaseViewHolder>.onItemClick(onItemClick: (item: T#1) → Unit): BaseQuickAdapter<T#1, KBaseViewHolder> defined in humazed.github.com.kotlinandroidutils
public fun <T, K : BaseViewHolder> BaseQuickAdapter<T#1, KBaseViewHolder>.onItemClick(onItemClick: (position: Int, item: T#1) → Unit): BaseQuickAdapter<T#1, KBaseViewHolder> defined in humazed.github.com.kotlinandroidutils

完整的code on GitHub

我知道SOF上也有类似的问题,但是它们与我的问题不同,答案在这里不适用。

0 个答案:

没有答案