tapchicoma
02/19/2019, 3:38 PMfun bar(): Single<List<Number>> {
return Single.just(listOf<Int>())
}
But this doesn't (type mismatch
):
fun bar(): Single<List<Number>> {
val foo = Single.just(listOf<Int>())
return foo
}
Removing Single
or changing to:
fun bar(): Single<List<Number>> {
val foo = Single.just<List<Number>>(listOf<Int>())
return foo
}
solves the issue 🤔streetsofboston
02/19/2019, 3:46 PMSingle
is defined as *in*variant:
public abstract class Single<T> ...
That means that in your 2nd example, this line
val foo = Single.just(listOf<Int>())
reads like
val foo: Single<List<Int>> = Single.just(listOf<Int>())
and since Single’s generic-param is invariant, a Single<List<Int>>
is not a subclass of Single<List<Number>>
If Single was defined as class Single<out E> ...
your 2nd example would compile. E.g. replace Single
with `Single2`:
class Single2<out E> {
companion object {
fun <E> just(value: E) : Single2<E> {
TODO()
}
}
}
foo
.
The 1st example, though, is different. I guess it can infer the upcast there, but not in the 2nd. That is somewhat odd.tapchicoma
02/19/2019, 3:52 PMfun bar(): Single<out List<Number>>() { .. }
also solves the issue, so seems you are right