https://kotlinlang.org logo
Title
f

farzad

11/24/2019, 8:23 AM
I have a class with some nullable properties
data class RequestModel(
    val description: String?
)
and a validation function
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:
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

Alowaniak

11/24/2019, 12:36 PM
f

farzad

02/23/2020, 9:13 AM
Yes, This technique can help. Thanks for sharing.