Files
kotlin-fork/compiler/testData/diagnostics/testsWithStdLib/inference/kt32345.kt
T
Mikhail Zarechenskiy 5582fd4056 [NI] Discard DefNotNull types inside invariant positions
#KT-30297 Fixed
 #KT-32168 Fixed
 #KT-27722 Fixed (actually, it was fixed with addition of DefNotNullTypes, and now test was added to save this behavior)
 #KT-32345 Fixed
2019-11-06 15:20:17 +03:00

50 lines
1.6 KiB
Kotlin
Vendored

// !DIAGNOSTICS: -UNUSED_VARIABLE
import kotlin.reflect.KProperty
import kotlin.properties.ReadWriteProperty
class CleanupTestExample {
val cleanUpBlocks: MutableList<Pair<Any, (Any) -> Unit>> = mutableListOf()
class CleaningDelegate<T : Any?>(
initialValue: T? = null,
val cleanupBlocks: MutableList<Pair<Any, (Any) -> Unit>>,
val block: (T) -> Unit
) : ReadWriteProperty<Any?, T> {
private var value: T? = initialValue
init {
addCleanupBlock(initialValue)
}
override fun getValue(thisRef: Any?, property: KProperty<*>): T {
return value ?: throw IllegalStateException("Property ${property.name} should be initialized before get.")
}
@Suppress("UNCHECKED_CAST")
override fun setValue(thisRef: Any?, property: KProperty<*>, value: T) {
addCleanupBlock(value)
this.value = value
}
fun addCleanupBlock(value: T?) {
if (value != null) {
@Suppress("UNCHECKED_CAST")
cleanupBlocks.add((<!DEBUG_INFO_SMARTCAST!>value<!> to block) as Pair<Any, (Any) -> Unit>)
}
}
}
data class TestHolder(val num: Int)
fun <T : Any?> cleanup(initialValue: T? = null, block: (T) -> Unit) = CleaningDelegate(initialValue, cleanUpBlocks, block)
fun testWithCleanup() {
val testHolder = TestHolder(1)
var thing: TestHolder by CleaningDelegate(testHolder, cleanupBlocks = cleanUpBlocks, block = { println("cleaning up $it") })
var thing2: TestHolder by cleanup(testHolder) { println("cleaning up $it") }
}
}