https://kotlinlang.org logo
Title
c

chirag

06/20/2018, 1:27 PM
data class User(
        val name: String? = null,
        val address: String? = null,
        val phone: String? = null
)
Suppose I have a data class like above I am validating properties name, address, phone as not null So if its not null how can i change its type to NotNullable because if i use this data class in anywhere else i always have to use !! or ? despite of its already validated for null check. so that data class after validation looks like
data class User(
        val name: String,
        val address: String,
        val phone: String
)
Anybody faced this problem ?
n

Nicolas Chaduc

06/20/2018, 1:31 PM
you can use :
data class User(
        val name: String = "",
        val address: String = "",
        val phone: String = ""
)
y

ylemoigne

06/20/2018, 1:58 PM
data class RawUser(val name:String?, ...){
	fun validate():User {
	  ....
	}
}

data class User(val name:String, ....)
Yes there is a lot of couling between the 2 class. But the other side of the tradeoff is that you encode model entity lifecycle into the type system, so the compiler help you to avoid error and as dev, at any place in system code, you have garantee about the lifecycle state of the entity.
c

chirag

06/20/2018, 6:16 PM
so still i have to use !! assertion while assigning to User data class properties, is there any way to assign properties or cast from String? to String @ylemoigne
y

ylemoigne

06/20/2018, 9:59 PM
I'm unsure to understand what you mean :/
g

gildor

06/21/2018, 5:18 AM
@chirag Could you please show some example how you constrcut this data class and why do you need
!!