Renan Kummer
09/09/2024, 9:07 PMif (myObj.getDate() == null || myObj.getDate().isBefore(...)) // java
looks in fact cleaner than
if (myObj.date == null || myObj.date!!.isBefore(...)) // Kotlin
since Kotlin insists that the module can change and thus can't guarantee non-nullability.
This would be fine for libraries, but the module where myObj is defined is part of my app and compiled together, it's just a matter of internal organization. Is there a way to enable a less strict smart casting?Youssef Shoaib [MOD]
09/09/2024, 9:19 PMephemient
09/09/2024, 9:20 PMinternal
visibility)ephemient
09/09/2024, 9:20 PMWout Werkman
09/10/2024, 8:39 AMmyObj.getDate() == null || myObj.getDate().isBefore(...)
is technically not safe given that you can not assume it's implementation (KT-50534 linked above is a proposal to allow the compiler to be able to assume some implementations).
Another thing that could solve this are value types. (but most likely will take a long time (but idk, I'm not involved in the compiler or design team))
The nice thing about value semantics is that they are not only easier for you to comprehend, but also for the compiler, allowing improved analysis like you're suggesting.
Since Kotlin can not offer something for imperative/Java style approach, here's some alternatives:
• If you are slowly getting used to Kotlin's more functional approach, you might like be able to live with: myObj.date.let { it != null || it.isBefore(...) }
.
• You could even make your own wrapper if you feel like making it read more natural: myObj.data.isNullOr { it.isBefore(...) }
.
• Alternatively (I personally would really not recommend this, it contains nullable booleans which are significantly harder to comprehend!), you could do: myObj.data?.isBefore(...) != false
.CLOVIS
09/10/2024, 10:00 AMval date = myObj.date
if (date == null || date.isBefore(…))
smart-casting will work there, since the value can't change (it's a local variable)hfhbd
09/10/2024, 10:03 AMRenan Kummer
09/10/2024, 6:06 PM