How to create empty constructor for data class in Kotlin?

Technology CommunityCategory: KotlinHow to create empty constructor for data class in Kotlin?
VietMX Staff asked 3 years ago

If you give default values to all the fields – empty constructor is generated automatically by Kotlin.

data class User(var id: Long = -1,
               var uniqueIdentifier: String? = null)

and you can simply call:

val user = User()

Another option is to declare a secondary constructor that has no parameters:

data class User(var id: Long,
               var uniqueIdentifier: String?){
    constructor() : this(-1, null)
}