is it desired behavior for reflection to be able t...
# announcements
s
is it desired behavior for reflection to be able to bypass kotlin null safety?
a
What do you mean with bypass?
prints "Stupid Kotlin2!!!!"
and the object is non nullable.
a
you are setting it to
null
with reflection?
s
well my coworker is, but yes.
I'm asking for him. I thought kotlin would verify this at runtime, but it seems not.
a
thats expected, the JVM doesn't know anything about nullable/non-nullable types
it does check it at the kotlin <-> java border, but not inside kotlin code
s
yeah, but kotlin generally inserts null checks. Why are we not receiving an NPE like if we used
!!
?
looking at the bytecode, kotlin generates a check for the
!!
a
imagine if kotlin would place null-checks everywhere inside the code, that's gonna decrease performance very heavily
s
I guess it would be crazy expensive to do that for every access of every object.
yeah.
but why not do it at the reflection boundary?
I actually don't know if he's using kotlin reflect, or java reflect... maybe that's why
a
I don't know, you have to ask the Jetbrains guys
s
seems like he's using java reflection. I'll have him try it with kotlin-reflect.
thanks!
m
reflection also lets you bypass final fields. Once you’re using reflection, it’s up to you to do the right thing
1
👆 3
👍🏽 1
g
I had a simular thing reading from a database. Kotlin asummed not null, but because the data was created from clojure, it was null, and gave a runtime error aborting the program.
s
Thanks. Yeah I assumed that was it, but had a coworker complaining 😉