Hey everyone. I have the following issue/question/suggestion:
Let’s assume that we have some class with the following function:
class AClass {
inline fun <reified T> get(): T = TODO()
}
And let’s also assume that we have some extension function on this class with the following function:
inline fun <reified T> AClass.wrapped(crossinline receiver: AClass.() -> T): Wrapper<T>
= Wrapper.wrap { receiver() }
Now, my common sense tells me that it is only natural if all of these functions are inlined and with reified generic types, I should be able to do the following:
fun foo() {
val a = AClass()
val wrapped: Wrapper<String> = a.wrapped { get() }
}
But apparently, I can’t. Compiler can infer the type that should be returned inside this lambda with receiver, but cannot auto cast the return function, which is also inlined and with reified generic type. So I have to do this:
fun foo() {
val a = AClass()
val wrapped: Wrapper<String> = a.wrapped { get<String>() }
}
I need this kind of functionality to add easy-to-use support of wrapping classes for the library I’m currently working on. So I want to understand why compiler behaves in that way. Is this intended behavior? If so, what lies behind the scenes? In not, should we create an issue ticket or is this kind of functionality is already staged for future releases?
Thanks to everyone in advance! I’m very dived into Kotlin, but this is my first discussion here.