Support sam conversion in ir interpreter

This commit is contained in:
Ivan Kylchik
2021-08-05 13:12:52 +03:00
committed by TeamCityServer
parent b85a796492
commit 23b315446f
12 changed files with 156 additions and 22 deletions
+10
View File
@@ -136,6 +136,16 @@ public fun <T, C : MutableCollection<in T>> Iterable<T>.toCollection(destination
return destination
}
public inline fun <T, R> Iterable<T>.map(transform: (T) -> R): List<R> {
return mapTo(ArrayList<R>(if (this is Collection<*>) this.size else 10), transform)
}
public inline fun <T, R, C : MutableCollection<in R>> Iterable<T>.mapTo(destination: C, transform: (T) -> R): C {
for (item in this)
destination.add(transform(item))
return destination
}
internal fun <T> List<T>.optimizeReadOnlyList() = when (size) {
0 -> emptyList()
1 -> listOf(this[0])
+32
View File
@@ -0,0 +1,32 @@
import kotlin.*
import kotlin.collections.*
@CompileTimeCalculation
fun interface IntPredicate {
fun accept(i: Int): Boolean
fun defaultMethod() = 1
}
const val isEven = IntPredicate { it % 2 == 0 }
.<!EVALUATED: `false, true, false, true, false`!>let { predicate -> listOf(1, 2, 3, 4, 5).map { predicate.accept(it) }.joinToString() }<!>
const val isOdd = IntPredicate { it % 2 != 0 }
.<!EVALUATED: `true, false, true, false, true`!>let { predicate -> listOf(1, 2, 3, 4, 5).map { predicate.accept(it) }.joinToString() }<!>
const val callToDefault = IntPredicate { false }.<!EVALUATED: `1`!>defaultMethod()<!>
const val callToString = IntPredicate { false }.<!EVALUATED: `(kotlin.Int) -> kotlin.Boolean`!>toString()<!>
@CompileTimeCalculation
fun interface KRunnable {
fun invoke(): String
}
@CompileTimeCalculation
object OK : () -> String {
override fun invoke(): String = "OK"
}
@CompileTimeCalculation
fun foo(k: KRunnable) = k.invoke()
const val invokeFromObject = <!EVALUATED: `OK`!>foo(OK)<!>
const val invokeFromFunInterface = <!EVALUATED: `OK`!>foo(KRunnable { "OK" })<!>