1

I have a kotlin data class:

data class MyCats (
) {
    val name: String = "",
    val female: Boolean = false,
    val fixed: Boolean = false
}

As I understand Kotlin (still a newbie), I can instantiate this class and set all its parameters at once, such as

val morris = MyCats("Morris")

Now let's say that I get morris fixed. I can't change the value of morris.fixed because it's a val. But I can create a new object. How do I make a new object with all the values of morris, but with the fixed set to true?

Sure, I could go through and do everything manually, but I thought the whole point of Kotlin was to save programmers from that sort of boilerplate code.

SMBiggs
  • 11,034
  • 6
  • 68
  • 83

1 Answers1

4

Call the copy function:

morris.copy(fixed = true)

Louis Wasserman
  • 191,574
  • 25
  • 345
  • 413
  • How did you know that? What other not-reserved-words do I need to know to use kotlin? My eyes must have glazed over while reading that part. The docs are currently 619 pages long (https://kotlinlang.org/docs/kotlin-docs.pdf); it'll take a while to memorize it. – SMBiggs Aug 11 '20 at 20:58
  • Don't necessarily try to memorize it, just focus on learning to look up relevant documentation efficiently. – Louis Wasserman Aug 11 '20 at 21:02
  • 1
    Or just let your IDE show you all the available methods and extension functions!  If you're using IntelliJ, type `morris.` and hit Ctrl+Space, and see what pops up… – gidds Aug 11 '20 at 22:25