[NI] Commonize type-conversions (SAM/Suspend)
- Allow participating subtypes of functional types in conversions - Fix several subtle inconsistencies - Place logic about conversions at one place Now conversions operations have two stages: before usual subtyping check and after one. This is needed to support conversions of subtypes (of functional types, for example). First, the compiler checks if it possible to resolve an argument without conversion and only then it tries to perform conversion. Note that it'd be incorrect to perform conversion eagerly as it can change resolve (Runnable & () -> Unit <: KRunnable), plus we can't guess whether conversion is needed at all as it's important not to look into supertypes if resolution doesn't actually needed it #KT-36448 Fixed #KT-37574 Fixed #KT-38604 Fixed
This commit is contained in:
@@ -0,0 +1,40 @@
|
||||
// TARGET_BACKEND: JVM
|
||||
// IGNORE_BACKEND_FIR: JVM_IR
|
||||
|
||||
// FILE: a.kt
|
||||
|
||||
object IntMapper : (Int) -> String {
|
||||
override fun invoke(value: Int): String {
|
||||
return value.toString()
|
||||
}
|
||||
}
|
||||
|
||||
// FILE: Sam.java
|
||||
|
||||
public interface Sam<T, R> {
|
||||
R apply(T t);
|
||||
}
|
||||
|
||||
// FILE: Foo.java
|
||||
|
||||
public class Foo {
|
||||
public static String bar1(IntMapper mapper) {
|
||||
return mapper.invoke(0);
|
||||
}
|
||||
|
||||
public static <T, R> R bar2(Sam<? super T, ? extends R> mapper, T input) {
|
||||
return mapper.apply(input);
|
||||
}
|
||||
}
|
||||
|
||||
// FILE: test.kt
|
||||
|
||||
fun box(): String {
|
||||
val a = Foo.bar1(IntMapper)
|
||||
if (a != "0") return "Failed 0: $a"
|
||||
|
||||
val b = Foo.bar2(IntMapper, 1)
|
||||
if (b != "1") return "Failed 1: $b"
|
||||
|
||||
return "OK"
|
||||
}
|
||||
Reference in New Issue
Block a user