https://kotlinlang.org logo
Title
m

Mateusz Konieczny

02/21/2023, 7:26 PM
Is it expected behavior? I thought that
Surface
is subset of
Surface?
so it will be expanded to it. If unusual or should not happen I will try to make a minimal reproducible example out of https://github.com/streetcomplete/StreetComplete/pull/4642#pullrequestreview-1304594775
For now I created
fun Surface.asItem(): DisplayItem<Surface> = Item(this, iconResId, titleResId)
and
fun Surface.asItemWithFakeNullPossibility(): DisplayItem<Surface?> = Item(this, iconResId, titleResId)
but I suspect there is a better way to handle this...
w

Wout Werkman

02/21/2023, 7:29 PM
If you did not specify that the generic parameter of
DisplayItem
is
out
, then this is expected. I think you want to add
out
m

Mateusz Konieczny

02/21/2023, 7:30 PM
I have not specified it, I guess I need to learn what
out
is doing
m

mkrussel

02/21/2023, 7:31 PM
Surface
is a subtype of
Surface?
but that doesn't make
DisplayItem<Surface>
a subtype of
DisplayItem<Surface?>
. The subtyping of the generic types are based on the variance. A good explanation. https://typealias.com/guides/illustrated-guide-covariance-contravariance/
m

Mateusz Konieczny

02/21/2023, 7:32 PM
Thanks for hints!
but that doesn't make
DisplayItem<Surface>
a subtype of
DisplayItem<Surface?>
I see that I need to learn more, I was expecting exact opposite (and got confused what is going on)
If you have a better place to learn about
out
than official docs - please let me know!
m

mkrussel

02/21/2023, 7:34 PM
I think typealias is the best place.
e

ephemient

02/21/2023, 7:34 PM
Producer<T>
is a subtype of
Producer<T?>
Consumer<T?>
is a subtype of
Consumer<T>
m

mkrussel

02/21/2023, 7:34 PM
But the
out
and
in
keywords change the variance of the generics which changes the subtyping rules.
e

ephemient

02/21/2023, 7:34 PM
Kotlin uses the terms
in
and
out
because that's where it goes
e.g. a consumer can take
in T
, a producer gives
out T
c

CLOVIS

02/21/2023, 9:13 PM
New users (of any language) are often confused by variance because we tend to think of immutable data structures, which are always covariant (a
List<Int>
is a
List<Number>
), but this is unsafe if the list can be modified
e

ephemient

02/21/2023, 9:26 PM
for historical (Java) reasons, arrays are covariant, even though it's unsafe