Language assistance for weak property

It feels like a new language should provide simplification of implementing a weak reference field. Currently, it seems like I must do something like this to have a WeakReference field that is settable from the outside:

class c {
    var delegate: WeakReference<X>? = null
    fun setDelegate(delegate: X) {
        this.delegate = WeakReference(delegate)
    // if you need (maybe you don't)
    fun getDelegate(): X? = delegate?.get()

Have you looked at delegated properties? That probably fills your needs.

1 Like