如何使用第四个源扩展 Observables.combineLatest() ?
想象一下,有两个带有字符串的 BehaviourSubject
:
private val subjectLocationA = BehaviorSubject.createDefault("London")
private val subjectLocationB = BehaviorSubject.createDefault("Paris")
我是否更新了每个位置
subjectLocationA.onNext("Denver")
一个枚举类
@Serializable
enum class ActiveLocationList(@StringRes val value: Int) {
ALL_LOCATION("all"),
MY_FAVLOCATION("favorites")
}
,然后我在 Observable 中使用了
val settingsActiveLocationList: Observable<ActiveLocationList>
:我将上面的 Subjects
和 Observables< /code> via
combineLatest()
:
Observables.combineLatest(
subjectLocationA,
subjectLocationB,
settingsActiveLocationList,
).switchMap { (locationA, locationB, activeLocationList) ->
//..
}
现在我想根据上面的示例扩展第三个位置主题 subjectLocationC
。但我即将扩展 mergeLatest:
Observables.combineLatest(
subjectLocationA,
subjectLocationB,
subjectLocationC,
settingsActiveLocationList,
).switchMap { (locationA, locationB, locationC, activeLocationList) ->
//..
IDE 抱怨
类型不匹配。
必需:(类型变量(T1),类型变量(T2),类型变量(T3))→类型变量(R)
发现:可观察
并且
无法推断此参数的类型。请明确指定。
根据 Observables
实现,除其他外
fun <T1 : Any, T2 : Any, T3 : Any> combineLatest
,
inline fun <T1 : Any, T2 : Any, T3 : Any, T4 : Any, R : Any> combineLatest
但没有介于两者之间?!我想我需要
fun <T1 : Any, T2 : Any, T3 : Any, T4 : Any> combineLatest
还是我错了?也许有解决方法吗?我缺少什么?
Imagine there are two BehaviourSubject
with Strings:
private val subjectLocationA = BehaviorSubject.createDefault("London")
private val subjectLocationB = BehaviorSubject.createDefault("Paris")
were I update location each like
subjectLocationA.onNext("Denver")
Then I have a enum class
@Serializable
enum class ActiveLocationList(@StringRes val value: Int) {
ALL_LOCATION("all"),
MY_FAVLOCATION("favorites")
}
used in a Observable:
val settingsActiveLocationList: Observable<ActiveLocationList>
I combine the above Subjects
and Observables
via combineLatest()
:
Observables.combineLatest(
subjectLocationA,
subjectLocationB,
settingsActiveLocationList,
).switchMap { (locationA, locationB, activeLocationList) ->
//..
}
Now I would like to extend a third location subject subjectLocationC
according to above example. But I am about to extend the combineLatest:
Observables.combineLatest(
subjectLocationA,
subjectLocationB,
subjectLocationC,
settingsActiveLocationList,
).switchMap { (locationA, locationB, locationC, activeLocationList) ->
//..
IDE complains with
Type mismatch.
Required: (TypeVariable(T1), TypeVariable(T2), TypeVariable(T3)) → TypeVariable(R)
Found: Observable
and
Cannot infer a type for this parameter. Please specify it explicitly.
According to the Observables
implementation says amongst others
fun <T1 : Any, T2 : Any, T3 : Any> combineLatest
and
inline fun <T1 : Any, T2 : Any, T3 : Any, T4 : Any, R : Any> combineLatest
but nothing in between?! I think I would need
fun <T1 : Any, T2 : Any, T3 : Any, T4 : Any> combineLatest
or am I wrong? Is there a workaround maybe? What am I missing?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

就我个人而言,我有一个库,但 Stack Overflow 不喜欢链接,所以我使用以下代码,你可以复制粘贴它
Personally I have a library for it, but Stack Overflow doesn't like links, so I use the following code, you can just copy-paste it