有没有办法在vararg参数中保留类型?

时间:2019-04-24 01:58:10

标签: android generics kotlin

我的问题是有可能做类似的事情:

fun <T, R> someFunction(vararg sources<out T>, doSomething: (vararg sources<out T>) -> R) {
    // do something here
}

所以,如果我做类似的事情:

someFunction(SomeType<A>(), SomeType<B>(), SomeType<C>()) { a: A, b: B c: C ->
    // do Something
}

基本上,高阶函数需要所有参数类型。

我问这个问题的原因是我想简化如下代码:

inline fun <T1, T2, R> MediatorLiveData<out R>.merge(source1: LiveData<out T1>, source2: LiveData<out T2>, crossinline merger: (T1?, T2?) -> R?) {
    addSource(source1) {
        this.value = merger.invoke(source1.value, source2.value)
    }
    addSource(source2) {
        this.value = merger.invoke(source1.value, source2.value)
    }
}

inline fun <T1, T2, T3, R> MediatorLiveData<out R>.merge(source1: LiveData<out T1>, source2: LiveData<out T2>, source3: LiveData<out T3>, crossinline merger: (T1?, T2?, T3?) -> R?) {
    addSource(source1) {
        this.value = merger.invoke(source1.value, source2.value, source3.value)
    }
    addSource(source2) {
        this.value = merger.invoke(source1.value, source2.value, source3.value)
    }
    addSource(source3) {
        this.value = merger.invoke(source1.value, source2.value, source3.value)
    }
}

inline fun <T1, T2, T3, T4, R> MediatorLiveData<out R>.merge(source1: LiveData<out T1>, source2: LiveData<out T2>, source3: LiveData<out T3>, source4: LiveData<out T4>, crossinline merger: (T1?, T2?, T3?, T4?) -> R?) {
    addSource(source1) {
        this.value = merger.invoke(source1.value, source2.value, source3.value, source4.value)
    }
    addSource(source2) {
        this.value = merger.invoke(source1.value, source2.value, source3.value, source4.value)
    }
    addSource(source3) {
        this.value = merger.invoke(source1.value, source2.value, source3.value, source4.value)
    }
    addSource(source4) {
        this.value = merger.invoke(source1.value, source2.value, source3.value, source4.value)
    }
}

有人可以提出建议吗?预先感谢!

1 个答案:

答案 0 :(得分:1)

尝试以下功能:

fun <T, R> someFunction(vararg sources: LiveData<out T>, doSomething: (sources: Array<out LiveData<out T>>) -> R) {
    // ...

    doSomething(sources)
}

似乎我们不能在lambda表达式vararg中使用修饰符doSomething,将其替换为Array即可。

编辑: 因此,基本上,您将能够对不同数量的源执行以下操作,而无需像示例中那样创建其他功能:

fun <T, R> someFunction(vararg sources: LiveData<out T>, doSomething: (sources: Array<out LiveData<out T>>) -> R) {
    sources.forEach {
        addSource(it) {
            doSomething.invoke(sources)
        }
    }
}

// Call someFunction with different number of args:
val l1: LiveData<Int> = MutableLiveData()
val l2: LiveData<String> = MutableLiveData()
val l3: LiveData<String> = MutableLiveData()

// Call with two args:
someFunction(l1, l2) { sources: Array<out LiveData<out Any>> ->
    val data1 = sources[0] as LiveData<Int>
    val data2 = sources[1] as LiveData<String>
    // do your work here   
}
// Or Call with three args:
someFunction(l1, l2, l3) { sources: Array<out LiveData<out Any>> ->
    val data1 = sources[0] as LiveData<Int>
    val data2 = sources[1] as LiveData<String>
    val data3 = sources[2] as LiveData<String>
    // do your work here   
}