I have a class with some nullable properties ```d...
# announcements
f
I have a class with some nullable properties
Copy code
data class RequestModel(
    val description: String?
)
and a validation function
Copy code
fun validate(model: RequestModel): RequestModel{
    if(model.description == null) throw IllegalArgumentException("description must be non null")
    return model
}
After this validation step, I need a way to indicate non-nullability of
description
property. One solution is to create a new data class which has non null propertis
data class RequestModel(val description: String)
. But I'm looking for a generic way to avoid creating new classes per use case. Ideal generic solution:
Copy code
fun validate(model: RequestModel): NoNullableField<RequestModel>
How can I remove nullability from properties of a c.lass with nullable properties in a generic way? Is it usefull to use some kind of kotlin compiler contract? Link to SO question: https://stackoverflow.com/questions/59015821
a
f
Yes, This technique can help. Thanks for sharing.