All Downloads are FREE. Search and download functionalities are using the official Maven repository.

main.com.sceyt.chatuikit.persistence.LazyVarDelegate.kt Maven / Gradle / Ivy

There is a newer version: 1.7.2
Show newest version
package com.sceyt.chatuikit.persistence

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

fun  lazyVar(initializer: () -> T): ReadWriteProperty {
    return LazyVarDelegate(initializer)
}

class LazyVarDelegate(initializer: () -> T) : ReadWriteProperty {
    private val defaultValue: T by lazy(initializer)
    private var overrideValue: T? = null

    override fun setValue(thisRef: Any?, property: KProperty<*>, value: T) {
        overrideValue = value
    }

    override fun getValue(thisRef: Any?, property: KProperty<*>): T {
        return overrideValue ?: defaultValue
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy