y
04/25/2023, 2:07 PMMap<K, V>.get(key)
work for key == null
? how does it even typecheck?ephemient
04/25/2023, 2:15 PMAdam S
04/25/2023, 2:17 PMephemient
04/25/2023, 2:17 PMoperator fun <K, V> Map<out K, V>.get(key: K): V?
with K = T?
, Map<T, V>
is a Map<out K, V>
y
04/25/2023, 2:22 PMK
is covariant to K?
?Joffrey
04/25/2023, 2:39 PMK
, but rather of the type using K
. But what you can say is that K
is a subtype of K?
y
04/25/2023, 2:40 PMMap<K, V>
is covariant to Map<K?, V>
?ephemient
04/25/2023, 2:41 PMMap
is not naturally covariant in K
but the get
extension is explicitly on out K
Joffrey
04/25/2023, 2:50 PMget
operator allows you to use Map<K,V>.get<K?, V>(nullableK)
because Map<out K, V>
is covariant in its first type parameter K
, which in turn means that Map<K,V>
is a subtype of Map<K?,V>
in this context.y
04/25/2023, 2:52 PMephemient
04/25/2023, 6:04 PMK
is a subtype of `K?`: every inhabitant of K
is also an inhabitant of K?
Map<K, V>
is a subtype of Map<out K?, V>
because K
satisfies out K?
because K
is a subtype of K?
Stephan Schröder
04/26/2023, 8:00 AMK: Any?