tschuchort
04/23/2019, 5:54 PMFoo<*> in Kotlin equivalent to Foo<?> in Java?karelpeeters
04/23/2019, 6:04 PMkarelpeeters
04/23/2019, 6:04 PMNote: star-projections are very much like Java's raw types, but safe.
starke
04/23/2019, 6:07 PMkarelpeeters
04/23/2019, 6:07 PMtschuchort
04/23/2019, 6:10 PMkarelpeeters
04/23/2019, 6:12 PMtschuchort
04/23/2019, 6:20 PM* at all when it seems to be equivalent to in Nothing?streetsofboston
04/23/2019, 6:33 PMlist: List<*> to still call list.size. Your function just needs to know it’s a list. It is not interested in what the list contains. And * is much less typing that in Nothing 🙂tschuchort
04/23/2019, 6:37 PMList<in Nothing> as well. I don't see any difference between the twostreetsofboston
04/23/2019, 6:40 PMval list: List<in Nothing> does not compile, because a list is defined as List<out T>. But val list: List<*> works fine.streetsofboston
04/23/2019, 6:41 PMMutableList, both in Nothing and * work, because MutableList<T> is invariant.Dias
04/23/2019, 6:41 PMtschuchort
04/23/2019, 6:58 PMFoo comes from Java or Kotlin and is invariant then Foo<?> is equivalent to Foo<in Nothing?>. But what if it comes from Kotlin and has in or out variance?ilya.gorbunov
04/23/2019, 7:49 PM*-projection is equivalent to both out U and in Nothing, where U is the upper bound of the generic parametertschuchort
04/23/2019, 7:56 PMvoid foo(Foo<?> f) and one in Kotlin fun bar(f: Foo<*>) then the IDE shows that both are of type (Foo<*>) -> Unit (in a Kotlin file) but the Java one accepts null and the Kotlin one doesn'tstreetsofboston
04/23/2019, 8:16 PMf parameter accepts a Foo<*>! type because it comes from java (note the !), which accepts null as valid values. The f parameter of the kotlin version accepts a Foo<*> type, which is non-nullabletschuchort
04/23/2019, 9:15 PMFoo<?> is directly converted to Foo<*>!? Why not, when it would be possible to encode the nullability?streetsofboston
04/23/2019, 9:23 PMT from java is converted to T! when used in Kotlin. The ! is denoting a platform type:
https://kotlinlang.org/docs/reference/java-interop.html#notation-for-platform-types
If you want to enforce nullable, non-nullable from Java, you should use the appropriate annotations in Java (if you have that option):
https://kotlinlang.org/docs/reference/java-interop.html#nullability-annotations