use declared upper bounds in type inference (not just check them)
- simple cases supported #KT-2856 fixed
This commit is contained in:
@@ -0,0 +1,20 @@
|
||||
//KT-2856 Fix the getOrElse signature to be able to return any supertype of V
|
||||
package d
|
||||
|
||||
import java.util.HashMap
|
||||
|
||||
public inline fun <K,V1, V: V1> Map<K,V>.getOrElse1(key: K, defaultValue: ()-> V1) : V1 {
|
||||
if (this.containsKey(key)) {
|
||||
return this.get(key) as V
|
||||
} else {
|
||||
return defaultValue()
|
||||
}
|
||||
}
|
||||
|
||||
fun main(args: Array<String>) {
|
||||
val map = HashMap<Int, Int>()
|
||||
println(map.getOrElse1(2, { null })) // Error
|
||||
}
|
||||
|
||||
//from standard library
|
||||
fun println(<!UNUSED_PARAMETER!>message<!> : Any?) {}
|
||||
+15
@@ -0,0 +1,15 @@
|
||||
package a
|
||||
|
||||
fun foo<V : U, U>(<!UNUSED_PARAMETER!>v<!>: V, u: U) = u
|
||||
fun bar<U, V : U>(<!UNUSED_PARAMETER!>v<!>: V, u: U) = u
|
||||
|
||||
fun test(a: Any, s: String) {
|
||||
val <!UNUSED_VARIABLE!>b<!>: Any = <!TYPE_INFERENCE_UPPER_BOUND_VIOLATED!>foo<!>(a, s) //depends on type parameter order, will be fixed in next commit
|
||||
val <!UNUSED_VARIABLE!>c<!>: Any = bar(a, s)
|
||||
}
|
||||
|
||||
fun baz<V : U, U>(<!UNUSED_PARAMETER!>v<!>: V, u: MutableSet<U>) = u
|
||||
|
||||
fun test(a: Any, s: MutableSet<String>) {
|
||||
<!TYPE_INFERENCE_UPPER_BOUND_VIOLATED!>baz<!>(a, s)
|
||||
}
|
||||
Reference in New Issue
Block a user