Skip to content

Instantly share code, notes, and snippets.

View johnsonlee's full-sized avatar
🎯
Focusing

Johnson Lee johnsonlee

🎯
Focusing
View GitHub Profile
@bnorm
bnorm / xref.kt
Last active November 29, 2020 08:41
One to many relationship via property delegation - Inspiration: https://github.com/jcornaz/xref
object OneToMany {
private class One<O, M, C : MutableCollection<M>>(initial: O?, private val rightProperty: KMutableProperty1<O, C>) : ReadWriteProperty<M, O?> {
var one: O? = initial
override fun getValue(thisRef: M, property: KProperty<*>): O? = one
override fun setValue(thisRef: M, property: KProperty<*>, value: O?) {
when {
value != null -> add(value, thisRef)