I was thinking about how obtuse it is to have weak...
# language-proposals
k
I was thinking about how obtuse it is to have weak references in Java, do you think Kotlin could simplify that by adding weak/strong references as apart of the declaration of a variable like
Copy code
weak var something: Something? = someRef //of type WeakReference<Something> 
if (something == null) { /*True if something's reference is null*/ } 
var inferredWeakSomething = something //of type WeakReference<Something>
strong var strongSomething = something //of type Something?
e
Why make it available only for weak refs? Why not support for all kinds of “boxes”. But Kotlin already has this feature — delegated variables. With a few libraries definitions you can already make it work like this:
Copy code
var something: Something by weak(someRef)
👍 1
d
@elizarov I noticed that the stdlib appears to be missing a property delegate for
ThreadLocal
. A simple
ThreadLocal
subclass implementing
ReadWriteProperty<Any?, T>
that is returned from some overloaded functions named
threadLocal
would be cool to have. Would a pull request into the stdlib repository be the right approach?
e
The right approach would be to implement it in your library. You can also vote for the corresponding issue: https://youtrack.jetbrains.com/issue/KT-10957
d
Fair enough. Thanks