https://kotlinlang.org logo
Title
b

bartvh

11/15/2018, 2:05 PM
I'm trying to define an inner type that has one, and exactly one, instance per outer instance (so not an
object
), which cannot be constructed elsewhere without reflection, but I can't seem to get it any tighter than
inner
. Making it
private
will not allow to me create the one instance:
inner class TheLock internal constructor()
    private val theLockInstance = TheLock()

    inline fun <T> synced(block: (TheLock) -> T) =
            synchronized(this) {
                block(theLockInstance)
            }
This is an experiment to see if I can get the compiler to help me make sure that particular functions only run when holding the lock
k

karelpeeters

11/15/2018, 2:09 PM