Effective Kotlin: Item 12 — Always override toString

In item 12 of Joshua Bloch’s remarkable book, Effective Java, he covers overriding toString. With Kotlin’s data class providing an implementation of toString you may think it unnecessary to write your own.

The toString produced in a data class lists each property in order of definition. The code below will produce the string “User(name=Bob, age=30)”:

data class User(val name: String, val age: Int)

println(User("Bob", 30))