56
data class Student(
    val id: Int?,
    val firstName: String?,
    val lastName: String?,
    val hobbyId: Int?,
    val address1: String?,
    val address2: String?,
    val created: String?,
    val updated: String?,
    ...
)

I have like above data class, and I want to create a Student instance with only first name and last name. So If I run this,

// creating a student 
Student(
    firstName = "Mark"
    lastName = "S"
)

I will get No value passed for parameter 'id' ... errors.

To avoid that, I modified the Student class like this,

data class Student(
    val id: Int? = null,
    val firstName: String? = null,
    val lastName: String? = null,
    val hobbyId: Int? = null,
    val address1: String? = null,
    val address2: String? = null,
    val created: String? = null,
    val updated: String? = null,
    ...
)

But it looks so ugly.

Is there any better way?

Expert wanna be
  • 10,218
  • 26
  • 105
  • 158
  • 4
    I think that's pretty much the only way to go. You could make `firstName` and `lastName` non-nullable, but that won't really change much on the class definition. – Edson Menegatti Nov 06 '17 at 10:00

3 Answers3

68

You can set default values in your primary constructor as shown below.

data class Student(val id: Int = Int.MIN_VALUE,
               val firstName: String,
               val lastName: String,
               val hobbyId: Int = Int.MIN_VALUE,
               val address1: String = "",
               val address2: String = "",
               val created: String = "",
               val updated: String = "")

Then you can use named arguments when creating a new student instance as shown below.

Student(firstName = "Mark", lastName = "S")
Newbie
  • 7,031
  • 9
  • 60
  • 85
27

I am not sure the solution I am giving you is the best or not. But definitely neat.

The only thing I don't like to go with nulls as default param, because Kotlin offers Null Safety, lets not remove it just because to fulfil some other requirement. Mark them null only if they can be null. Else old Java way is good. Initialize them with some default value.

data class Student(val id: Int,
                   val firstName: String,
                   val lastName: String,
                   val hobbyId: Int,
                   val address1: String,
                   val address2: String,
                   val created: String,
                   val updated: String) {
    constructor(firstName: String, lastName: String) :
            this(Int.MIN_VALUE, firstName, lastName, Int.MIN_VALUE, "", "", "", "")
}
Hardik Trivedi
  • 5,677
  • 5
  • 31
  • 51
  • 21
    I don't think that null safety means not to use nulls. I think its better to think of null safety as not getting a nullpointerexception in places where you don't expect it. By adding a question mark we inform every user of the class to expect a null value and add measures to deal with it. I personally think that a null is more appropriate for a non-provided address instead of an empty string. – PaulB Mar 29 '19 at 08:24
  • What if we have to pass other parameters in another place? For that, we need to create one more constructor. – Prashant Dange Jan 17 '23 at 05:12
1
data class InLog(
var clock_in_lat: String?="None",
var clock_in_lng: String?="None",
var clock_out_lat: String?="None",
val clock_out_lng: String?="None",
val created_at: String?="None",
val duration: String?="None",
val end_time: String?="None",
val id: Int?=Int.MIN_VALUE,
var late_duration: String? = "None",
val start_time: String?="None",
val type: String?="None",
val updated_at: String?="None",
val user_id: Int?=Int.MIN_VALUE) 

in Kotlin we do like that remeber ? mark symbol use.

Umesh Yadav
  • 1,042
  • 9
  • 17