> Optionally ignoring thread safety seems indefensible
Nobody forces everything to be thread safe. That would either be suicidally complex or suicidally slow. The problem isn't so much "ignoring thread safety", it's that this:
class Foo(var thing: Int?) {
fun doSomething() {
if (thing != null) {
thing++
}
}
}
doesn't compile and
it should. It fails to compile claiming that 'thing' could have changed in the meantime, so it can't safely assume non-null. However, that can only happen if Foo is accessed from multiple threads, but this isn't thread safe nor pretending to be in the first place. You can make the compiler happy by doing this:
class Foo(var thing: Int?) {
fun doSomething() {
val t = thing
if (t != null) {
thing = t + 1
}
}
}
But of course that's not remotely thread safe, either. It wasn't thread safe to begin with, and it's still not thread safe now. But this will of course compile just fine. You're forced to jump through hoops to workaround compiler "bugs"
To be fair here the warning isn't actually about thread safety at all, it's to guard against something like this:
class Foo(var thing: Int?) {
fun otherThing() {
thing = null
}
fun doSomething() {
if (thing != null) {
otherThing()
thing++
}
}
}
Kotlin has so far just been unwilling to allow the compiler to handle the cases where it could prove that the variable hasn't been modified in the meantime because they can't always prove it.