【问题标题】:Kotlin lambda syntax confusionKotlin lambda 语法混淆
【发布时间】:2016-01-23 18:14:59
【问题描述】:

我对 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&lt;kotlin.Throwable&gt;, Action0)

Note: subscribe is RxJava method

编辑 1

class GroupRecyclerViewAdapter(private val groups: MutableList<Group>,
                           private val listener: OnListFragmentInteractionListener?) :
RecyclerView.Adapter<GroupRecyclerViewAdapter.ViewHolder>(), Action1<ArrayList<Group>> {

【问题讨论】:

  • 看起来像类型引用错误
  • 如果您可以将错误消息包含为文本,那就太好了,这将有助于其他人找到此帖子。您可以编译,以获得您在编辑器中看到的相同错误。然后剪切粘贴
  • @JaysonMinard 添加。谢谢

标签: kotlin rx-kotlin


【解决方案1】:

view.adapter as GroupRecyclerViewAdapter 部分应该是 lambda func,而不是 Action,因为 onError 和 onComplete 也是 lambdas

所以,修复这个尝试:

.subscribe(
          { (view.adapter as GroupRecyclerViewAdapter).call(it) }
          , { println(it.message) }
          , { println("completed") }
      )

用你的名字(用你的类型替换Unit

class GroupRecyclerViewAdapter : Action1<Unit> {
    override fun call(t: Unit?) {
        print ("onNext")
    }
}

使用 lambdas

val ga = GroupRecyclerViewAdapter()
...subscribe(
    { result -> ga.call(result) },
    { error -> print ("error $error") },
    { print ("completed") })

有动作

...subscribe(
    ga,
    Action1{ error -> print ("error $error") },
    Action0{ print ("completed") })

选择一个

【讨论】:

  • 不,不起作用。如果是这样,那为什么我的最后一个 sn-p 有效呢?
  • 因为在上一个示例中它们都是操作。给我一秒钟我试试
  • 请至少提供view.adapter签名。真的是Action1吗?如果在我的示例中应该是(view.adapter as GroupRecyclerViewAdapter).call(it),对不起
  • 当然。添加它。这是行动1
  • 嘿伙计们,它有效还是无效?不将 SAM 与 lambda 一起使用的规则从何而来?没听说过。
【解决方案2】:

您有两个版本的 subscribe 方法可供选择:

  • 第一个(真正的)有签名 subscribe(Action1&lt;ArrayList&lt;Group&gt;&gt;, Action1&lt;Throwable&gt;, Action0)
  • 第二个版本由 Kotlin 编译器生成,具有 签名subscribe((ArrayList&lt;Group&gt;&gt;) -&gt; Unit, (Throwable) -&gt; Unit, () -&gt; Unit)

但是,在您的代码中,您传递了以下参数类型:

subscribe(
    view.adapter as GroupRecyclerViewAdapter, // Action1<Throwable>
    { println(it.message) },  // (Throwable) -> Unit
    { println("completed") } // () -> Unit
)

如您所见,这些参数类型不满足任何可用的签名。另一个答案为您提供了一些解决问题的方法。此外,您可以让GroupRecyclerViewAdapter 实现函数类型Function1&lt;ArrayList&lt;Group&gt;, Unit&gt;(它们也是接口)而不是Action1&lt;ArrayList&lt;Group&gt;&gt;

【讨论】:

  • 如果能混搭就好了!
猜你喜欢
  • 2019-01-03
  • 2016-09-28
  • 2014-02-04
  • 1970-01-01
  • 1970-01-01
  • 2017-03-13
  • 2019-05-09
  • 2015-07-11
  • 2013-10-09
相关资源
最近更新 更多