Allow delegating val properties to out-projected MutableMap
Change generic signature of MutableMap.getValue, use 'out @Exact V' and `V1: V` types instead of single `in V`. Fix affected IR generation tests. #KT-18789 Fixed
This commit is contained in:
@@ -26,8 +26,8 @@ public inline operator fun <V, V1: V> Map<in String, @Exact V>.getValue(thisRef:
|
||||
*/
|
||||
@kotlin.jvm.JvmName("getVar")
|
||||
@kotlin.internal.InlineOnly
|
||||
public inline operator fun <V> MutableMap<in String, in V>.getValue(thisRef: Any?, property: KProperty<*>): V
|
||||
= @Suppress("UNCHECKED_CAST") (getOrImplicitDefault(property.name) as V)
|
||||
public inline operator fun <V, V1: V> MutableMap<in String, out @Exact V>.getValue(thisRef: Any?, property: KProperty<*>): V1
|
||||
= @Suppress("UNCHECKED_CAST") (getOrImplicitDefault(property.name) as V1)
|
||||
|
||||
/**
|
||||
* Stores the value of the property for the given object in this mutable map.
|
||||
|
||||
@@ -5,6 +5,8 @@ import kotlin.test.*
|
||||
class ValByMapExtensionsTest {
|
||||
val map: Map<String, String> = hashMapOf("a" to "all", "b" to "bar", "c" to "code")
|
||||
val genericMap = mapOf<String, Any?>("i" to 1, "x" to 1.0)
|
||||
val mmapOut: MutableMap<String, out String> = mutableMapOf("g" to "out", "g1" to "in")
|
||||
val genericMmapOut: MutableMap<String, out Any?> = mmapOut
|
||||
|
||||
val a by map
|
||||
val b: String by map
|
||||
@@ -12,6 +14,8 @@ class ValByMapExtensionsTest {
|
||||
val d: String? by map
|
||||
val e: String by map.withDefault { "default" }
|
||||
val f: String? by map.withDefault { null }
|
||||
val g: String by mmapOut
|
||||
val g1: String by genericMmapOut
|
||||
// val n: Int by map // prohibited by type system
|
||||
val i: Int by genericMap
|
||||
val x: Double by genericMap
|
||||
@@ -23,6 +27,8 @@ class ValByMapExtensionsTest {
|
||||
assertEquals("code", c)
|
||||
assertEquals("default", e)
|
||||
assertEquals(null, f)
|
||||
assertEquals("out", g)
|
||||
assertEquals("in", g1)
|
||||
assertEquals(1, i)
|
||||
assertEquals(1.0, x)
|
||||
assertFailsWith<NoSuchElementException> { d }
|
||||
|
||||
Reference in New Issue
Block a user