Initialise class without defining the optional values

Raoul :

I have a question about initialising classes. I have a Profile data class which I want to initialise without defining the optional values. How can I do that? When I make data class as followed:

data class Profile(
  val id: Long,
  val email: String,
  val password: String,
  val photo: String?
)

val profile = Profile(id = 1, email = "", password = "")

My IDE is giving an error about that the photo is not defined. I also don't want to set a default value, cause it should be possible that the photo is null, and I don't want to override that.

Leo Aso :

If you want it to be null by default, then you have to explicitly set it to null, there's no way around that. One aspect in which Kotlin purposely differs from Java is that no variable is ever given a implicit default value. Even if it is nullable, you are required to set it to null yourself.

data class Profile(
  val id: Long,
  val email: String,
  val password: String,
  val photo: String? = null
)

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=309340&siteId=1