A Kotlin data class must have a primary constructor that defines at least one member. Other than that, you can add secondary constructors as explained in Classes and Inheritance - Secondary Constructors.
For your class, and example secondary constructor:
data class User(val name: String, val age: Int) {
constructor(name: String): this(name, -1) { ... }
}
Notice that the secondary constructor must delegate to the primary constructor in its definition.
Although many things common to secondary constructors can be solved by having default values for the parameters. In the case above, you could simplify to:
data class User(val name: String, val age: Int = -1)
Default values in the primary constructor eliminates many needs for secondary constructors, but if the needed instance depends on logic based on data that must be analyzed the better answer may be to use a companion object.
data class KeyTag(val a: String, val b: Int, val c: Double) {
companion object Factory {
val empty = KeyTag("", 0, 0.0)
fun create(bigString: String): KeyTag {
// Logic to extract appropriate values for arguments a, b, c
return KeyTag(a, b, c)
}
fun bake(i: Int): KeyTag = KeyTag("$i", i, i.toDouble())
}
}
Usage is then:
val ks = KeyTag.create("abc:1:10.0")
val ke = KeyTag.empty
val kb = KeyTag.bake(2)
Instructs the Kotlin compiler to generate overloads for this function that substitute default parameter values.
If a method has N parameters and M of which have default values, M overloads are generated: the first one takes N-1 parameters (all but the last one that takes a default value), the second takes N-2 parameters, and so on.
data class User @JvmOverloads constructor(
var email: String="",
var password: String="")
I wanted to have a class similar to below (with a constructor that parses an input)
data class Data(val a: String, val b: String) {
constructor(spaceSeparated: String) { // error because we don't call this()
val split = spaceSeparated.split(" ")
this(split.first(), split.last()) // error again because it's not valid there
}
}
The solution is to do this:
data class Data(val a: String, val b: String) {
companion object {
operator fun invoke(spaceSeparated: String): Data {
val split = spaceSeparated.split(" ")
return Data(split.first(), split.last())
}
}
}
And it can be called just as if it were a constructor
Yes you can have multiple contractors on data classes. But there is something which makes the primary constructor special. The compiler will auto generate methods like equals, hashCode, copy, toStrings based on the primary constructor for the data class.
Below is an example of two instances of a data class which looks different (first.gender = male, second.gender = female) but equals method would evaluate to true because gender is not defined in the primary constructor and therefore not considered in the auto generated methods. Likewise, gender would not be included in the string representation.
data class A(val name: String, val age: Int) {
var gender: String = "Female"
constructor(name: String, age: Int, gender: String) : this(name, age) {
this.gender = gender
}
}
fun main(args: Array<String>) {
val first = A("foo", 10)
val second = A("foo", 10, "Male")
println(first == second) //prints true
println(second) //prints A(name=foo,age=10)
}