I am trying to define some methods which resolves ...
# random
a
I am trying to define some methods which resolves overloading like so:
Copy code
inline 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?
g
Refined will not help in this case because resolution of which method should be called in
apply
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:
Copy code
when (apply) {
 is String -> on(value)
 …
}
In this case smartcast resolve correct methods
👍 1