Created
September 28, 2022 13:07
-
-
Save green-nick/33fdcb525d65e2b5124ea8d713f7c923 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import kotlin.properties.ReadWriteProperty | |
import kotlin.reflect.KProperty | |
fun <T> threadLocal(init: () -> T) = object : ReadWriteProperty<Any?, T> { | |
private val internal = object : ThreadLocal<T>() { | |
override fun initialValue() = init() | |
} | |
override fun getValue(thisRef: Any?, property: KProperty<*>): T = internal.get()!! | |
override fun setValue(thisRef: Any?, property: KProperty<*>, value: T) { | |
internal.set(value) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment