Observer pattern using a set of observers and delegated properties The idea is to implement the observer pattern, with a separate notification method for each observed property of the observed object. Let me start with a simplified use case:

class Point {
    val observers: ObserverSet&lt;PointObserver&gt;()
    val x by observers.observe(0f, PointObserver::xChanged)
    val y by observers.observe(0f, PointObserver::yChanged)
}

interface PointObserver {
    fun xChanged(old: Float, new: Float)
    fun yChanged(old: Float, new: Float)
}

class PointView(point: Point) {
    init {
        point.observers.add(object : PointObserver {
            override fun xChanged(old: Float, new: Float) { ... }
            override fun yChanged(old: Float, new: Float) { ... }
        })
    }
}


I'm new to Kotlin, so I'd like some feedback on the following implementation:

import kotlin.properties.ReadWriteProperty
import kotlin.reflect.KProperty

/**
 * A set of observers for a particular object, to easily implement the Observer Pattern without needing inheritance.
 *
 * This can be made a public property of the object. Call [observe] to create delegated properties for each property
 * that observers need to be notified about.
 *
 * @param O The type of the observers, typically an interface.
 */
class ObserverSet&lt;O&gt; {

    private val observers = mutableSetOf&lt;O&gt;()
    private val properties = mutableListOf&lt;ObservedProperty&lt;*&gt;&gt;()

    /**
     * Sets whether an observer is present in the set. If the observer was actually added to the set, its notification
     * methods will be called with the current value of the property.
     *
     * @param observer The observer.
     * @param present If true, the observer will be added to the set if not already there. If false, the observer will
     *   be removed from the set if it's there.
     */
    fun toggle(observer: O, present: Boolean) {
        if (present) {
            val wasPresent = observer in observers
            observers.add(observer)
            if (!wasPresent) {
                init(observer)
            }
        } else {
            observers.remove(observer)
        }
    }

    /**
     * Creates a delegate property with a callback that notifies each observer.
     *
     * @param V The type of the property.
     * @param initialValue The initial value of the property.
     * @param onChanged Function on the [O] interface that will be called with the old and new value after the
     *   property has been changed.
     */
    fun &lt;V&gt; observe(initialValue: V, onChanged: O.(old: V, new: V) -&gt; Unit): ReadWriteProperty&lt;Any?, V&gt; {
        val property = ObservedProperty(initialValue, onChanged)
        properties.add(property)
        return property
    }

    private fun init(observer: O) {
        properties.forEach { property -&gt;
            property.initObserver(observer)
        }
    }

    private inner class ObservedProperty&lt;V&gt;(initialValue: V, private val onChanged: O.(old: V, new: V) -&gt; Unit) :
            ReadWriteProperty&lt;Any?, V&gt; {

        private var value = initialValue

        override fun getValue(thisRef: Any?, property: KProperty&lt;*&gt;): V {
            return value
        }

        override fun setValue(thisRef: Any?, property: KProperty&lt;*&gt;, value: V) {
            val oldValue = this.value
            this.value = value
            observers.forEach { observer -&gt;
                observer.onChanged(oldValue, value)
            }
        }

        fun initObserver(observer: O) {
            observer.onChanged(value, value)
        }
    }
}


(The toggle method is the only one I need right now; regular add and remove could of course be implemented as well.)

I'm mainly interested in efficiency considerations, ways to make this more general, and following Kotlin best practices.
You don't need a wasPresent variable. MutableSet.add returns true if the element was added to the set:

if (observers.add(observer)) {
    init(observer)
}

Sometimes in Kotlin you don't have to declare temporary variables so that you can do something with a return value before returning it. e.g.:

return ObservedProperty(initialValue, onChanged).apply { properties.add(this) }


Unfortunately this can sometimes look a bit strange using this, etc. but in Kotlin 1.1 you can use bound callable references:

return ObservedProperty(initialValue, onChanged).apply(properties::add)

You might want to take a look at Delegates.observable - stdlib - Kotlin Programming Language which you may be able to reuse to improve and reduce your code.