Michael de Kaste
05/01/2025, 8:22 AMdata class Request(
val type: Type
val value: String?
)
enum class Type { A, B }
data class A(val value: String)
data class B(val value: String?)
fun Request.toDto() = when(type){
Type.A if value == null -> throw ApiValidationException("value has to be filled when type is A")
Type.A -> A(value) // <-- Cannot infer value being non-null
Type.B -> B(value)
}
dmitriy.novozhilov
05/05/2025, 6:51 AMType.A if value == null
and Type.A
are more or less independent branches. The smartcast engine is not smart enough cover this case.
Feel free to report a feature-request for this case at kotl.in/issue