我对 Kotlin lambda 语法感到困惑。
起初,我有
.subscribe(
{ println(it) }
, { println(it.message) }
, { println("completed") }
)
效果很好。
然后我将 onNext 移动到另一个名为 GroupRecyclerViewAdapter 的类,它实现了Action1<ArrayList<Group>>
.
.subscribe(
view.adapter as GroupRecyclerViewAdapter
, { println(it.message) }
, { println("completed") }
)
但是,我得到了错误:
Error:(42, 17) Type mismatch: inferred type is () -> ??? but rx.functions.Action1<kotlin.Throwable!>! was expected
Error:(42, 27) Unresolved reference: it
Error:(43, 17) Type mismatch: inferred type is () -> kotlin.Unit but rx.functions.Action0! was expected
我可以通过更改为:
.subscribe(
view.adapter as GroupRecyclerViewAdapter
, Action1<kotlin.Throwable> { println(it.message) }
, Action0 { println("completed") }
)
有没有办法在不指定类型的情况下编写 lambda?( Action1<kotlin.Throwable>
, Action0
)
编辑 1
class GroupRecyclerViewAdapter(private val groups: MutableList<Group>,
private val listener: OnListFragmentInteractionListener?) :
RecyclerView.Adapter<GroupRecyclerViewAdapter.ViewHolder>(), Action1<ArrayList<Group>> {