Inline callable references

#KT-6900 Fixed
This commit is contained in:
Michael Bogdanov
2015-04-10 12:20:40 +03:00
parent 991db29731
commit e200b97ca4
19 changed files with 235 additions and 23 deletions
@@ -0,0 +1,7 @@
import test.*
fun box() : String {
val call = call(A(11), A::calc)
return if (call == 11) "OK" else "fail"
}
@@ -0,0 +1,9 @@
package test
class A(val z: Int) {
fun calc() = z
}
inline fun call(p: A, s: A.() -> Int): Int {
return p.s()
}
@@ -0,0 +1,7 @@
import test.*
fun box() : String {
val call = A(11).test()
return if (call == 11) "OK" else "fail"
}
@@ -0,0 +1,11 @@
package test
class A(val z: Int) {
fun calc() = z
fun test() = call(A(z), ::calc)
}
inline fun call(p: A, s: A.() -> Int): Int {
return p.s()
}
@@ -0,0 +1,7 @@
import test.*
fun box() : String {
val call = call(11, ::A)
return if (call == 11) "OK" else "fail"
}
@@ -0,0 +1,9 @@
package test
class A(val z: Int) {
fun calc() = z
}
inline fun call(p: Int, s: (Int) -> A): Int {
return s(p).z
}
@@ -0,0 +1,5 @@
import test.*
fun box() : String {
return if (call("123", String::length) == 3) "OK" else "fail"
}
@@ -0,0 +1,5 @@
package test
inline fun call(p: String, s: String.() -> Int): Int {
return p.s()
}
@@ -0,0 +1,9 @@
import test.*
fun box() : String {
return if (call(10, ::calc) == 5) "OK" else "fail"
}
fun calc(p: Int) : Int {
return p / 2
}
@@ -0,0 +1,5 @@
package test
inline fun call(p: Int, s: (Int) -> Int): Int {
return s(p)
}
@@ -0,0 +1,9 @@
import test.*
fun box() : String {
return if (call(10, Int::calc) == 100) "OK" else "fail"
}
fun Int.calc(p: Int) : Int {
return p * this
}
@@ -0,0 +1,5 @@
package test
inline fun call(p: Int, s: Int.(Int) -> Int): Int {
return p.s(p)
}