https://kotlinlang.org logo
Title
j

joelpedraza

05/20/2019, 1:27 PM
Is there some annotation magic I can sprinkle on a val property to signal to the compiler that it’s guaranteed to be immutable (as apposed to only read only). Use case: cross module nullability smart casts
1
s

Stephan Schroeder

05/20/2019, 2:39 PM
const val
isn’t an option? (only works with compile-time constants) Apart from that I’m not aware of anything.
j

joelpedraza

05/20/2019, 2:41 PM
@Stephan Schroeder No it’s not an option, these are runtime values. Dumb data classes where I’d like to make guarantees about the immutability of fields
data class Person(val name: String?)
I want to guarantee name will always return the same value, was hoping there would be a contract or annotation for it
s

Stephan Schroeder

05/20/2019, 2:43 PM
then you’re out of luck, as far as I know.
j

joelpedraza

05/20/2019, 2:45 PM
As an aside, why doesn’t
@JvmField
on a val work this way
s

Stephan Schroeder

05/20/2019, 2:47 PM
I’m a bit out of my depth but that just results in no getter being created, but field level access in the byte-code as far as I know. so nothing in regards to immutability.
I watched the closing panel of the Kotlin conf 2018, and on the question along the lines of “which features would you like to see in Kotlin” one participant said “more immutability support”

https://www.youtube.com/watch?v=heqjfkS4z2I

j

joelpedraza

05/20/2019, 2:59 PM
A final field is referentially immutable, a val can be a getter
class Foo { val bar get() = Random.nextInt() }
s

Stephan Schroeder

05/20/2019, 3:25 PM
i know
j

joelpedraza

05/20/2019, 3:27 PM
Oh, of course. Because multiplatform.
m

Matej Drobnič

05/21/2019, 7:19 PM
yeah cross module immutability issues are pretty PITA. It sucks that there is no way to tell the compiler "I know what I'm doing I will not suddenly change these without recompilation"