oh yay I just got to my first "a mutable property ...
# getting-started
k
oh yay I just got to my first "a mutable property that could have been changed by this time" error.
h
use
let
k
yeah, that looks like it's probably the best option for this case
but leaves me wondering, is there any way to declare something to be run single-threaded, so we don't need
let
to make copies?
h
make a new val and make it equal the var
a
@keturn you can't declare that in kotlin
t
Isn’t there something in contracts that is going to help with that?
h
Not for mutable values
s
@keturn even if it was possible, it opens the door for bugs say if someone new to the code base didn't understand that single threaded assumption. Compile time constructs are a bliss
1
a
Kotlin won't have such a feature in the near future, because it can lead to nasty bugs when the compiler can't prove that the annotation is still correct https://youtrack.jetbrains.com/issue/KT-20294
k
To keep it a compile-time construct the compiler would somehow have to be able to trust that the object was thread-local. (or if shared, the only thread that may mutate it is the current thread) and yeah it may well be we don't have any way to communicate that in an enforceable way to the compiler.
thanks for the link to the youtrack ticket, the discussion there seems very thorough.
(and I'm very sympathetic to Mitja's point that there are environments like Kotlin/JS that are always single-threaded.)
I also realized that
let
is not copying the value of the property, but probably just a reference to that current value, which is a little less worrying resource-wise.
a
Why does the property have to be
var
?