Kotlin reflection is not available

Pravin Sonawane picture Pravin Sonawane · Jun 3, 2017 · Viewed 7.1k times · Source

I was trying to learn higher order functions from the first example of this video. Here's my code and output.

Code

fun lowercase(value: String) = value.toLowerCase()

fun higherOrder(value:String, op: (String) -> String) : String {
    println("Executing higher order fun $op")
    return op(value)
}

fun main(args: Array<String>) {
    println(higherOrder("HELLO", ::lowercase))
    println(higherOrder("hello", {it -> lowercase(it)}))
    println(higherOrder("HeLlo", { x -> lowercase(x) }))
    println(higherOrder("Hello", { lowercase(it) }))
}

Output

Executing higher order fun function lowercase (Kotlin reflection is not available)
hello
Executing higher order fun Function1<java.lang.String, java.lang.String>
hello
Executing higher order fun Function1<java.lang.String, java.lang.String>
hello
Executing higher order fun Function1<java.lang.String, java.lang.String>
hello

Process finished with exit code 0

So my question is, why does it print Kotlin reflection is not available?

Answer

Kiskae picture Kiskae · Jun 3, 2017

Full reflection requires the kotlin-reflect library in addition to kotlin-stdlib. If full reflection is available it will probably have a more comprehensive toString(), hence the message.