diff --git a/libraries/stdlib/src/kotlin/properties/Delegation.kt b/libraries/stdlib/src/kotlin/properties/Delegation.kt index 879aa8f7c0f..057c8cde922 100644 --- a/libraries/stdlib/src/kotlin/properties/Delegation.kt +++ b/libraries/stdlib/src/kotlin/properties/Delegation.kt @@ -55,9 +55,9 @@ public object Delegates { * @param map the map where the property values are stored. * @param default the function returning the value of the property for a given object if it's missing from the given map. */ - public fun mapVar(map: MutableMap, - default: (thisRef: Any?, desc: String) -> T): ReadWriteProperty { - return FixedMapVar(map, propertyNameSelector, default) + public fun mapVar(map: MutableMap, + default: (thisRef: R, propertyName: String) -> T): ReadWriteProperty { + return FixedMapVar(map, propertyNameSelector, default) } /** @@ -76,9 +76,9 @@ public object Delegates { * @param map the map where the property values are stored. * @param default the function returning the value of the property for a given object if it's missing from the given map. */ - public fun mapVal(map: Map, - default: (thisRef: Any?, desc: String) -> T): ReadOnlyProperty { - return FixedMapVal(map, propertyNameSelector, default) + public fun mapVal(map: Map, + default: (thisRef: R, propertyName: String) -> T): ReadOnlyProperty { + return FixedMapVal(map, propertyNameSelector, default) } } diff --git a/libraries/stdlib/test/properties/delegation/MapDelegationTest.kt b/libraries/stdlib/test/properties/delegation/MapDelegationTest.kt index 1d4b51ca4f3..0b1d7d60f00 100644 --- a/libraries/stdlib/test/properties/delegation/MapDelegationTest.kt +++ b/libraries/stdlib/test/properties/delegation/MapDelegationTest.kt @@ -8,6 +8,7 @@ import kotlin.test.* data class B(val a: Int) + class MapValWithDifferentTypesTest() { val map = hashMapOf("a" to "a", "b" to 1, "c" to B(1), "d" to null) val a by Delegates.mapVal(map) @@ -75,11 +76,13 @@ class MapValWithDefaultTest() { val a: String by Delegates.mapVal(map, default = { ref, desc -> "aDefault" }) val b: String by FixedMapVal(map, default = { ref: MapValWithDefaultTest, desc: String -> "bDefault" }, key = {"b"}) val c: String by FixedMapVal(map, default = { ref: MapValWithDefaultTest, desc: String -> "cDefault" }, key = { desc -> desc.name }) + val d: String by Delegates.mapVal(map, default = { ref, name -> ref.c }) @test fun doTest() { - assertTrue(a == "aDefault", "fail at 'a'") - assertTrue(b == "bDefault", "fail at 'b'") - assertTrue(c == "cDefault", "fail at 'c'") + assertEquals("aDefault", a, "fail at 'a'") + assertEquals("bDefault", b, "fail at 'b'") + assertEquals("cDefault", c, "fail at 'c'") + assertEquals(c, d, "'d' must have the default value equal to 'c'") } } @@ -88,17 +91,20 @@ class MapVarWithDefaultTest() { var a: String by Delegates.mapVar(map, default = {ref, desc -> "aDefault" }) var b: String by FixedMapVar(map, default = {ref: Any?, desc: String -> "bDefault" }, key = {"b"}) var c: String by FixedMapVar(map, default = {ref: Any?, desc: String -> "cDefault" }, key = { desc -> desc.name }) + var d: String by Delegates.mapVar(map, default = { ref, name -> ref.c }) @test fun doTest() { - assertTrue(a == "aDefault", "fail at 'a'") - assertTrue(b == "bDefault", "fail at 'b'") - assertTrue(c == "cDefault", "fail at 'c'") + assertEquals("aDefault", a, "fail at 'a'") + assertEquals("bDefault", b, "fail at 'b'") + assertEquals("cDefault", c, "fail at 'c'") + assertEquals(c, d, "'d' must have the default value equal to 'c'") a = "a" b = "b" c = "c" - assertTrue(a == "a", "fail at 'a' after set") - assertTrue(b == "b", "fail at 'b' after set") - assertTrue(c == "c", "fail at 'c' after set") + assertEquals("a", a, "fail at 'a' after set") + assertEquals("b", b, "fail at 'b' after set") + assertEquals("c", c, "fail at 'c' after set") + assertEquals(c, d, "'d' must still have the default value equal to 'c'") } } @@ -126,6 +132,15 @@ class MapPropertyFunctionTest() { } } +@Suppress("ALWAYS_NULL") +private val topLevel: String by Delegates.mapVal(emptyMap(), default = { ref, name -> ref.toString() }) + +class MapTopLevelVarWithDefaultTest() { + @test fun doTest() { + assertEquals("null", topLevel) + } +} + val mapVal = object : MapVal() { override fun map(ref: MapPropertyCustomTest) = ref.map override fun key(property: KProperty<*>) = "${property.name}Desc" @@ -152,14 +167,14 @@ val mapValWithDefault = object : MapVal) = property.name - override fun default(ref: MapPropertyCustomWithDefaultTest, key: KProperty<*>) = "default" + override fun default(ref: MapPropertyCustomWithDefaultTest, property: KProperty<*>) = "default" } val mapVarWithDefault = object : MapVar() { override fun map(ref: MapPropertyCustomWithDefaultTest) = ref.map override fun key(property: KProperty<*>) = property.name - override fun default(ref: MapPropertyCustomWithDefaultTest, key: KProperty<*>) = "default" + override fun default(ref: MapPropertyCustomWithDefaultTest, property: KProperty<*>) = "default" } class MapPropertyCustomWithDefaultTest() {