3

以下代码是我尝试将 RxJava 示例转换为 Kotlin。它应该将一堆Int' 收集到 aMutableList中,但我得到了很多错误。

val all: Single<MutableList<Int>> = Observable
        .range(10, 20)
        .collectInto(::MutableList, MutableList::add)

错误:

    Error:(113, 36) Kotlin: Type inference failed: Not enough information to infer parameter T in inline fun <T> MutableList(size: Int, init: (index: Int) -> T): MutableList<T>
Please specify it explicitly.

Error:(113, 49) Kotlin: One type argument expected for interface MutableList<E> : List<E>, MutableCollection<E> defined in kotlin.collections

    Error:(113, 67) Kotlin: None of the following functions can be called with the arguments supplied: 
public abstract fun add(element: Int): Boolean defined in kotlin.collections.MutableList
public abstract fun add(index: Int, element: Int): Unit defined in kotlin.collections.MutableList

如果我更改ImmutableList::addImmutableList<Int>::add,我摆脱了类型参数预期的错误,它被替换为:

Error:(113, 22) Kotlin: Type inference failed: fun <U : Any!> collectInto(initialValue: U!, collector: ((U!, Int!) -> Unit)!): Single<U!>!
        cannot be applied to
        (<unknown>,<unknown>)

这是 Java 中以下内容的直接副本:

Observable<List<Integer>> all = Observable
    .range(10, 20)
    .collect(ArrayList::new, List::add);

我知道第一个错误告诉我它要么推断出不正确的类型,我需要更明确(在哪里?),但我认为这::MutableList相当于() -> MutableList<Int>. 第三个错误是告诉我它不能调用任何add()带有参数的方法,但同样,我认为这MutableList::add相当于{ list, value -> list.add(value) }. 第四个错误告诉我它无法确定应用于collector.

如果我改用 lambda 表达式,则没有错误:

val all: Single<MutableList<Int>> = Observable
        .range(10, 20)
        .collectInto(mutableListOf(), { list, value -> list.add(value) })

all.subscribe { x -> println(x) }

我很感激一些关于我在方法引用上做错了什么的评论,因为很明显我误解了一些东西(通过Kotlin Language Reference,我想知道它现在是否甚至是一种语言功能?)。非常感激。

4

1 回答 1

3

在您的第一个示例中,您尝试将方法签名应用于collectfrom collectInto

这永远不会起作用,因为collect期望 aFunc0<R>和 a Action2<R, ? super T>并且collectInto期望一个真实的对象和 a BiConsumer<U, T>
构造函数引用不起作用collectInto- 你需要一个真实的对象(例如你的mutableListOf()调用)

第二个问题是 Kotlin 期望的是一个BiConsumer对象而不是一个函数。我不太清楚为什么。显然 Kotlin 无法处理来自 SAM 接口的 lambda 和函数引用的多个泛型。

因此,您需要传递一个实例BiConsumer而不仅仅是一个函数。
这也是我在评论中询问您是否确定错误消息的原因:

range(10, 20).collectInto(mutableListOf(), { l, i ->  l.add(i) }) 

会给我一个错误,而

range(10, 20).collectInto(mutableListOf(), BiConsumer { l, i ->  l.add(i) })

惯于。

于 2017-06-16T10:51:46.510 回答