The reflection is gone, and the properties are no ...
# language-evolution
d
The reflection is gone, and the properties are no longer adding performance or bytecode overhead, as they’re eliminated before the Kotlin is even compiled to Java. This would require the compiler to be able to make many more optimizations, but it would allow coders to write reflection-like code without deciding between the performance overhead of reflection or the engineering overhead of annotation processors. It could be used to write a custom
equals
or
hashCode
or
toString
implementation that uses fields based on their annotations or types, and to write easily understood serialization and deserialization formats. Another example. If properties are more aggressively inlined, it would also simplify existing code that uses properties. Consider this use of map delegation:
Copy code
class MapExample(
       map: Map<String, String>
) {
   val x by map
}
When the
by
is inlined, it becomes (effectively):
Copy code
val x: String get() = map.getOrImplicitDefault(MapExample::x.name)
The decompiled class contains an entire
KPropertyObject
that is used only for its name. Suppose that this name access were inlined, because the compiler knows that the name of
x
is “x”:
Copy code
val x: String get() = map.getOrImplicitDefault("x")
Now the overhead of involving properties is entirely eliminated. As I work on mobile code where reductions in bytecode size and execution time are top priorities, I would like to have property-inlining features like these to be able to use reflection-like features without their large overhead (We’ve eliminated many uses of
by
because of the unnecessary
KProperty
fields). Would these be feasible for the language, and how useful would they be to you?
h
I can't remeber where i read about it, but such a compile time reflection api was already mentioned by someone from jetbrains. Inlining on steroids or so. Regarding compiler optimizations, i don't know how far they currently go or will go in the future but the delegated properties use case is mentioned for Kotlin 1.4 https://blog.jetbrains.com/kotlin/2019/12/what-to-expect-in-kotlin-1-4-and-beyond/#more-7623
d
Exciting, I'll have to watch the keynote, thanks!