is there a reason why guards cannot infer nullabil...
# getting-started
m
is there a reason why guards cannot infer nullability based on previous checked values? Or is my IDE not understanding correctly? (I have when guards on and K2 compiler selected, it only complains about type inference)
Copy code
data 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)
}
d
From the compiler point of view
Type.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