Make ReadOnlyProperty and PropertyDelegateProvider fun interfaces

This commit is contained in:
Ilya Gorbunov
2020-06-02 00:24:30 +03:00
parent d2ea108123
commit de6154980d
4 changed files with 32 additions and 8 deletions
@@ -1,5 +1,5 @@
/*
* Copyright 2010-2018 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Copyright 2010-2020 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
*/
@@ -64,3 +64,27 @@ class VetoablePropertyTest {
assertFalse(result, "fail4: result should be false")
}
}
private typealias ReadOnlyPropertyDelegateProvider<T, V> =
PropertyDelegateProvider<T, ReadOnlyProperty<T, V>>
class DelegationInterfacesTest {
val a by ReadOnlyProperty { _, property -> property.name }
private val delegatedToProvider = mutableListOf<String>()
private val provider = ReadOnlyPropertyDelegateProvider { thisRef: DelegationInterfacesTest, property ->
val index = thisRef.delegatedToProvider.size
thisRef.delegatedToProvider.add(property.name)
ReadOnlyProperty { _, prop -> "${prop.name} is numbered $index" }
}
val b by provider
val c by provider
@Test fun doTest() {
assertEquals("a", a)
assertEquals(listOf("b", "c"), delegatedToProvider)
assertEquals("b is numbered 0", b)
assertEquals("c is numbered 1", c)
}
}