https://kotlinlang.org logo
Title
d

dimsuz

05/02/2017, 11:53 AM
Hi! I wonder if using kotlin delegates automatically means using reflection mechanisms. They have KProperty and all... Reflection brings performance costs on Android, that's why I am concerned about this.
m

miha-x64

05/02/2017, 11:57 AM
dimsuz:
KProperty
is just an interface which is an easy template for writing new delegates. Kotlin (unlike Groovy, for example) compiles statically, without any sudden reflection. Delegate is just an object who takes responsibility for storing a value.
d

dimsuz

05/02/2017, 11:58 AM
oh, so If i use
KProperty::name
in my delegate this won't end up in reflection calls?
m

miha-x64

05/02/2017, 12:05 PM
Kotlin reflection != Java reflection. Using KProperties leads to synthetic class generation, not to runtime annotation lookup.
d

dimsuz

05/02/2017, 12:05 PM
Nice! Can I read more about this generation somewhere?
m

miha-x64

05/02/2017, 12:07 PM
I don’t know, decompiler is my guide to Kotlin bytecode world 🙂 You can try out Tools -> Kotlin -> Show Kotlin Bytecode if you’re familiar with the JVM bytecode syntax.
d

dimsuz

05/02/2017, 12:08 PM
yep, I did decompile my delegate, but I guess I should try to decompile a class which uses it. thanks for the hint 🙂
m

miha-x64

05/02/2017, 12:09 PM
You may want to look at getter/setter code which uses synthetic classes directly.