When writing validation function, often times these validation functions check for nullability. Even though, after calling these functions an objects members can be safely used as if they were non-nullable, Kotlin compiler contracts don't allow specifying this behavior.
For instance:
data class EmployeeDto(
val name: String?,
val longitude: String?,
val latitude: String?
) {
@ExperimentalContracts
fun validate() {
//contract { returns() implies (this.name != null && this.longitude != null && this.latitude != null) }
// Error in contract description: only references to parameters are allowed in contract description
if (name.isNullOrEmpty())
throw RequiredValueNotSetException("Name was set to \"$name\" but can not be null or empty")
if (longitude.isNullOrEmpty())
throw RequiredValueNotSetException("Longitude was set to \"$longitude\" but can not be null or empty")
if (latitude.isNullOrEmpty())
throw RequiredValueNotSetException("Latitude was set to \"$latitude\" but can not be null or empty")
}
What is the rationale behind this constraint or is this is a feature planned to be added in the future?