From 93c73500d7be402beaf677633a7f5a8c01bd88e9 Mon Sep 17 00:00:00 2001 From: Ilya Gorbunov Date: Sat, 10 Oct 2015 03:43:54 +0300 Subject: [PATCH] Do not allow to delegate readonly property to a map with incompatible value type. Infer type of delegated readonly property from the map value type. --- libraries/stdlib/src/kotlin/properties/MapAccessors.kt | 2 +- .../stdlib/test/properties/delegation/MapAccessorsTest.kt | 7 +++++-- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/libraries/stdlib/src/kotlin/properties/MapAccessors.kt b/libraries/stdlib/src/kotlin/properties/MapAccessors.kt index 2fa8ce13766..49dad03a858 100644 --- a/libraries/stdlib/src/kotlin/properties/MapAccessors.kt +++ b/libraries/stdlib/src/kotlin/properties/MapAccessors.kt @@ -11,7 +11,7 @@ package kotlin.properties * * @throws NoSuchElementException when the map doesn't contain value for the property name and doesn't provide an implicit default (see [withDefault]). */ -public fun Map.getValue(thisRef: Any?, property: PropertyMetadata): V = getOrImplicitDefault(property.name) as V +public fun Map.getValue(thisRef: Any?, property: PropertyMetadata): V1 = getOrImplicitDefault(property.name) as V1 /** * Returns the value of the property for the given object from this mutable map. diff --git a/libraries/stdlib/test/properties/delegation/MapAccessorsTest.kt b/libraries/stdlib/test/properties/delegation/MapAccessorsTest.kt index a4f4336b018..a6e08864858 100644 --- a/libraries/stdlib/test/properties/delegation/MapAccessorsTest.kt +++ b/libraries/stdlib/test/properties/delegation/MapAccessorsTest.kt @@ -9,11 +9,13 @@ class ValByMapExtensionsTest { val map: Map = hashMapOf("a" to "all", "b" to "bar", "c" to "code") val genericMap = mapOf("i" to 1, "x" to 1.0) - val a: String by map + val a by map val b: String by map val c: Any by map val d: String? by map - val e: String? by map.withDefault { "default" } + val e: String by map.withDefault { "default" } + val f: String? by map.withDefault { null } + // val n: Int by map // prohibited by type system val i: Int by genericMap val x: Double by genericMap @@ -23,6 +25,7 @@ class ValByMapExtensionsTest { assertEquals("bar", b) assertEquals("code", c) assertEquals("default", e) + assertEquals(null, f) assertEquals(1, i) assertEquals(1.0, x) assertTrue(assertFails { d } is NoSuchElementException)