JVMLS 2015 - Flexible Types in Kotlin
Why Stack Overflow recommends it
Stack Overflow users link it to explain why Kotlin treats Java return values as platform types instead of always nullable, and what happens when a platform type is assigned to a non-null type.
Recommended by 3 people across 3 different months, 2017 to 2023.
What it is
A 2015 JVM Language Summit talk by Kotlin lead designer Andrey Breslav on flexible types, the mechanism behind Kotlin's platform types (the String! notation) for calling Java code. It explains how Kotlin treats Java types that may or may not be null and why this compromise was chosen.
What people said
Recommends lampenlampen · 2017-08 · Stack Overflow · on “Make Kotlin warn on assignment of flexible/platform type to non-null type?”
If you want to have more information why JetBrains introduced the platform types, there is a good video of Andrey Breslav (see: [this video] ). He gives also a explanation why they didn't go the safest way.
Recommends Marko Topolnik · 2018-05 · Stack Overflow · on “Null property when accessing lazy non-nullable property of Android Sensor”
Here's a specific moment in the talk you mention: [this video] The silde says "`String <- String!` --- fails if the value is null".
Recommends Sweeper · 2023-11 · Stack Overflow · on “kotlin compile always assume java method return nullable value, unless method annotate @NonNull”
This is not possible. To quote a JetBrains Team member from a similar discussion [LINK] on Kotlin Forums: platform types are not, and are not going to be, always nullable The same person also mentioned this talk [this video] by Andrey Breslau, which explained this decision. To summarise, this would introduce a lot of ?. s and !! s when calling Java code. It becomes a lot more inconvenient when you want to assert generic type parameters to be non-null. If a Java method returns List<String> , it would become List<String?>? if it treated all Java types as nullable. Although you can assert that the list is not null with !! , you cannot do the same to assert that String s in the list are also non-null
Cites Aro · 2018-05 · Stack Overflow · on “Null property when accessing lazy non-nullable property of Android Sensor”
Should there be a Compile-time error? Shouldn't it raise a compile time error when assigning a nullable to a val which is non-null? Yes, but that's not what's happening here In this case, you are assigning a platform type [LINK] to your value and telling the compiler to treat it as non-nullable. There should be a runtime error of IllegalStateException -as @MarkoTopolnik points out, this is a bug . Bug This bug is covered by KT-8135 [LINK] . Here's a linked issue directly related to delegates, KT-24258 [LINK] . There's also a discussion topic with some runnable examples here [LINK] . When will the runtime exception be thrown? The error will be triggered when you use proximitySensor in a way t