Ketlin reified type parameter as function parameter

I have a built-in function with a parameter of type reified with lambda that returns a new instance of an anonymous class that has one method that takes this reified type parameter and passes it to lambda.

inline fun <reified T : Any> eventHandler(crossinline handler: (T) -> Unit) = object {
    @Handler
    fun event(event: T) = handler(event)
}

      

Used like eventHandler<ExampleEvent> { println(it.data); }

, and everything compiles.

However, by examining the generated bytecode of the class generated by the specified call, I get this for the event method:

public final void event(example.ExampleEvent);
    descriptor: (Ljava/lang/Object;)V
    [...]
    Signature: #53 // (Lexample.ExampleEvent;)V

      

Therefore, when it correctly remembers the type in the Signature, it discards it in the descriptor; so thatEventMethod.getParameterTypes()

will have Object

instead of ExampleEvent

.

Is this a bug or intended behavior? Also, if this is intended, could there be some other way to achieve my goal (to prevent this clumsy object from being created all over the place, add a dummy method with @Handler, etc.)?

+3
kotlin


source to share


No one has answered this question yet

Check out similar questions:

510
What is the equivalent of Java static methods in Kotlin?
383
Conditional Kotlin Operator
205
Val and Var in Kotlin
92
Kotlin: how to pass a function as a parameter to another?
7
Ketlin reified type parameter is not used
five
How type erase is erased
3
Reated Kotlin type parameter cannot be used as type parameter in function body
1
Kotlin: type parameter Reified makes Gson fail
0
Can't compile extension function with type parameter reified in Kotlin
0
Invalid output for type parameter reified in Kotlin



All Articles
Loading...
X
Show
Funny
Dev
Pics