https://kotlinlang.org logo
Title
k

keturn

10/13/2018, 4:28 AM
oh yay I just got to my first "a mutable property that could have been changed by this time" error.
h

Hamza

10/13/2018, 4:29 AM
use
let
k

keturn

10/13/2018, 4:29 AM
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

Hamza

10/13/2018, 4:33 AM
make a new val and make it equal the var
a

Andreas Sinz

10/13/2018, 8:24 AM
@keturn you can't declare that in kotlin
t

tddmonkey

10/13/2018, 10:32 AM
Isn’t there something in contracts that is going to help with that?
h

Hamza

10/13/2018, 12:17 PM
Not for mutable values
s

Sam

10/13/2018, 1:04 PM
@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

Andreas Sinz

10/13/2018, 1:44 PM
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

keturn

10/13/2018, 4:39 PM
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

Andreas Sinz

10/13/2018, 10:02 PM
Why does the property have to be
var
?