Sam Stone
synchronized
val prop by lazy {}
var
by LazyMutable {}
class LazyMutable<T>(val initializer: () -> T) : ReadWriteProperty<Any?, T> { private object UNINITIALIZED_VALUE private var prop: Any? = UNINITIALIZED_VALUE @OptIn(InternalCoroutinesApi::class) @Suppress("UNCHECKED_CAST") override fun getValue(thisRef: Any?, property: KProperty<*>): T { return if (prop == UNINITIALIZED_VALUE) { synchronized(this) { return if (prop == UNINITIALIZED_VALUE) initializer().also { prop = it } else prop as T } } else prop as T } override fun setValue(thisRef: Any?, property: KProperty<*>, value: T) { synchronized(this) { prop = value } } }
mbonnin
Vidmantas Kerbelis
private val mutex = Mutex() mutex.withLock { } runBlocking { mutex.withLock { } } // Maybe an option, I can't guarantee that this is a good approach
A modern programming language that makes developers happier.