From de6154980d3f16ccf2bf0fa194ae74802dd51dbc Mon Sep 17 00:00:00 2001 From: Ilya Gorbunov Date: Tue, 2 Jun 2020 00:24:30 +0300 Subject: [PATCH] Make ReadOnlyProperty and PropertyDelegateProvider fun interfaces --- .../stdlib/api/js-v1/kotlin.properties.kt | 4 +-- libraries/stdlib/api/js/kotlin.properties.kt | 4 +-- .../src/kotlin/properties/Interfaces.kt | 6 ++--- .../properties/delegation/DelegationTest.kt | 26 ++++++++++++++++++- 4 files changed, 32 insertions(+), 8 deletions(-) diff --git a/libraries/stdlib/api/js-v1/kotlin.properties.kt b/libraries/stdlib/api/js-v1/kotlin.properties.kt index 200492c1bd7..429645ae93a 100644 --- a/libraries/stdlib/api/js-v1/kotlin.properties.kt +++ b/libraries/stdlib/api/js-v1/kotlin.properties.kt @@ -19,11 +19,11 @@ public abstract class ObservableProperty : kotlin.properties.ReadWritePropert } @kotlin.SinceKotlin(version = "1.4") -public interface PropertyDelegateProvider { +public fun interface PropertyDelegateProvider { public abstract operator fun provideDelegate(thisRef: T, property: kotlin.reflect.KProperty<*>): D } -public interface ReadOnlyProperty { +public fun interface ReadOnlyProperty { public abstract operator fun getValue(thisRef: T, property: kotlin.reflect.KProperty<*>): V } diff --git a/libraries/stdlib/api/js/kotlin.properties.kt b/libraries/stdlib/api/js/kotlin.properties.kt index 200492c1bd7..429645ae93a 100644 --- a/libraries/stdlib/api/js/kotlin.properties.kt +++ b/libraries/stdlib/api/js/kotlin.properties.kt @@ -19,11 +19,11 @@ public abstract class ObservableProperty : kotlin.properties.ReadWritePropert } @kotlin.SinceKotlin(version = "1.4") -public interface PropertyDelegateProvider { +public fun interface PropertyDelegateProvider { public abstract operator fun provideDelegate(thisRef: T, property: kotlin.reflect.KProperty<*>): D } -public interface ReadOnlyProperty { +public fun interface ReadOnlyProperty { public abstract operator fun getValue(thisRef: T, property: kotlin.reflect.KProperty<*>): V } diff --git a/libraries/stdlib/src/kotlin/properties/Interfaces.kt b/libraries/stdlib/src/kotlin/properties/Interfaces.kt index 52c38251410..a8e9ea9b3d7 100644 --- a/libraries/stdlib/src/kotlin/properties/Interfaces.kt +++ b/libraries/stdlib/src/kotlin/properties/Interfaces.kt @@ -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. */ @@ -16,7 +16,7 @@ import kotlin.reflect.KProperty * @param T the type of object which owns the delegated property. * @param V the type of the property value. */ -public interface ReadOnlyProperty { +public fun interface ReadOnlyProperty { /** * Returns the value of the property for the given object. * @param thisRef the object for which the value is requested. @@ -63,7 +63,7 @@ public interface ReadWriteProperty : ReadOnlyProperty { * @param D the type of property delegates this provider provides. */ @SinceKotlin("1.4") -public interface PropertyDelegateProvider { +public fun interface PropertyDelegateProvider { /** * Returns the delegate of the property for the given object. * diff --git a/libraries/stdlib/test/properties/delegation/DelegationTest.kt b/libraries/stdlib/test/properties/delegation/DelegationTest.kt index 59f87cadb70..d8f58a28915 100644 --- a/libraries/stdlib/test/properties/delegation/DelegationTest.kt +++ b/libraries/stdlib/test/properties/delegation/DelegationTest.kt @@ -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 = + PropertyDelegateProvider> + +class DelegationInterfacesTest { + val a by ReadOnlyProperty { _, property -> property.name } + + private val delegatedToProvider = mutableListOf() + 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) + } +} \ No newline at end of file