Albert
09/12/2018, 8:12 AMinline fun <reified T> apply(apply: T) {
on(apply)
}
fun on(apply: Any?) {
throw Exception()
}
fun on(apply: String) {
println("String: $apply")
}
fun on(apply: Int) {
println("Int: $apply")
}
When apply("test")
it will always endup in on(apply: Any?)
. I thought that inline
with reified
was basically a "copy/paste" behavior of that method. So when performing apply("test")
I would expect that after compiling it would look like something like so on("test")
. Clearly I am making a wrong assumption here.
Is it possible to do something like this? Or should I always use a reflection to resolve this at runtime?gildor
09/12/2018, 8:26 AMapply
made during compile time, it is not like a macros, which is just replace string on runtime, this will actually be compiled to bytecode and bytecode has static resolution>
Instead you can do something like:
when (apply) {
is String -> on(value)
…
}
In this case smartcast resolve correct methods