import kotlin.reflect.KProperty
class NullableSetterDelegate<T> {
private var store: T? = null
operator fun getValue(thisRef: Any?, property: KProperty<*>): T? = store
operator fun setValue(thisRef: Any?, property: KProperty<*>, value: T?) {
if (store == value) {
store = null
}
else {
store = value
}
}
}
class C {
var foo: String? by NullableSetterDelegate()
}
fun main(args: Array<String>) {
val c = C()
c.foo = "abc"
c.foo = "abc"
println(c.foo)
}