Is there a contract to let Kotlin know that my mem...
# announcements
d
Is there a contract to let Kotlin know that my member `val`'s value will stay the same? Basically I need smart casting to work on
thing
in
data class Wrapper(val thing: String?)
(from another module of course).
3
m
It will work as long as the property doesn't have a custom getter, or is delegated.
d
From another module? I don't think so.
m
Why would it work differently from another module @diesieben07?
d
Because of separate compilation. Someone might compile something with your module and your property is in a data-class. You then switch that property to be a computed one (custom getter), which means the smart-cast would no longer be allowed, but the other module is already compiled with the smart-cast in place.
From the docs:
val properties - if the property is private or internal or the check is performed in the same module where the property is declared. Smart casts aren't applicable to open properties or properties that have custom getters
m
Seems a bit weird to me. It wouldn't be any different than, let's say, removing a function, right? If the other module was already compiled with a call to that function, it would throw a
NoSuchMethodError
. In this case, I think it would throw an NPE because of the null-check the compiler inserts when it smart casts.
So, changing the property to a custom getter or delegate would just be a breaking change.
But that might just be what that rule avoids though.
d
So, changing the property to a custom getter or delegate would just be a breaking change.
Yes, this is a tradeoff. The Kotlin creators chose to make this not a breaking change
Its like changing a function implementation
m
Right 👍