dimsuz
09/20/2021, 1:51 PMdata class Value(val foo: Foo?)
and I do if (value.foo != null) foo.bar() else doStuff()
, there's an error "smart cast is impossible".
But if I know that a) it's val b) no multithreading is used, can I "smarten" the compiler? Perhaps by adding some contract
somewhere? Is there a YouTrack issue along these lines maybe?dmitriy.novozhilov
09/20/2021, 1:59 PMdata class Value(val foo: Foo?)
, and then, sometimes in future on server you updated that library to newer version, which also includes change foo
from val
to var
.dimsuz
09/20/2021, 2:19 PMdmitriy.novozhilov
09/20/2021, 2:23 PMephemient
09/20/2021, 5:27 PMvalue class
Tomasz Krakowiak
09/21/2021, 4:35 AM// data class A (val a : String)
data class A private constructor (val b : Int) {
val a : String
get() = rendomChar().repeat(b)
constructor (a : String) : this(a.length)
}
But I would love an annotation that complier would understand that would imply public contract that a getter or a method is pure and it's result won't be affected by any change of object's or global state.ephemient
09/21/2021, 8:05 AMvalue class
cannot be changed in such a way without breaking binary and source compatibilityTomasz Krakowiak
09/21/2021, 8:08 AMephemient
09/21/2021, 8:09 AMTomasz Krakowiak
09/21/2021, 8:17 AMvalue classes
and not data classes
. So I agree about binary compatibility. About source compatibility - wouldn't this dirty trick do - https://pl.kotl.in/KVcsMPy28 ?