Hi everybody.
As most of you might know, Kotlin enforces you to make local copies of mutable properties in order to gain deterministic null-safety (or overrule the nullability with ?.
or !!.
). In multi-threaded environments or in special code flows properties might easily change from not null to null between the check and actual usage.
Take this small example which causes the error
class Test {
var x:String? = "Hello World"
fun test() {
if(this.x != null) {
doSomething(this.x)
}
}
fun doSomething(x:String) {
println(x)
}
}
After reading on various discussions about Kotlin, it seems the devs and language-designers of Kotlin stand behind the decision of reporting those errors and do not even want to provide an opt-out for that as “you can never be fully safe”.
The guys from Roslyn (.net compiler) are working currently on introducing nullable-reference-types to the languages. So far they do not consider mutable properties in their nullability checks that’s why I submitted a feature request to opt-in for those kind of checks. The discussions there are going into a direction that this whole behavior is just noise and annoyance because thread safety must be guaranteed differently by the devs and having those kind of checks are not reasonable at all.
As a developer I am now seriously confused as 2 big players in programming languages claim something very contradictive. The Roslyn guys claim that a nullability check compiler feature should not have anything to do with multi-threading. I provided various examples with code flows that show multi-threading and code flow issues that would generate compiler-warning-free code but runtime exceptions, and still they do not believe that my arguments are valid.
Can somebody from the Kotlin world please make some statements on what was the rationale behind the decision of considering mutable properties always as unsafe on multiple accesses? Am I missing some big difference and both sides are right or is it simply a more philosophical question where the .net devs have a different POV than the Kotlin devs?
I would really appreciate if some Kotlin experts can shed some light on this topic
Kind Regards
Daniel