That’s exactly what I’d expect. And that’s why I was surprised to see that the code in my first post (the Int::class.java
variant) compiled fine and without any single warning and then failed at runtime. What’s funny, it fails at getProperty()
, that is, it’s perfectly fine to store an int
, but impossible to get it back because Integer
cannot be cast to Int
.
Come to think of, it… why? If Int
is a Kotlin (not Java) class that corresponds to Java’s int
, then why Class<Int>.cast()
doesn’t work for values of type Integer
? Is it just the way Java type system works? That is, Integer
is not a subtype of Int
, and that’s why the cast is impossible? And there’s no way to override that behavior because Class
is still a type from Java standard library that works the way Java tells it to and there is no way to force it to be a little more gentle about such cross-language casts?
Or is it possible, just wasn’t done in Kotlin for some reason? For example, it would have undesirable side effects or break some important principles?