Jonathan Olsson
05/13/2022, 12:45 PMtoString()
for non-debugging/non-logging purposes in a situation like this:
value class AccessToken(private val s: String) {
override fun toString(): String = s
}
val header = "Bearer $accessToken"
Given that inline classes most often wrap primitive-ish types, in my mind it falls into the same category as Long.MIN_VALUE.toString()
. Wdyt?Fleshgrinder
05/13/2022, 12:47 PMtoString
logic from Java where there is only one way for objects to auto-format themselves. In Java the function is not used consistently, think UUID
which prints itself, giving you good reason to do so as well.
Other languages (e.g. Rust) have support for different self-formatting strategies, but here we don't. I would go with it.Sam
05/13/2022, 12:48 PMJonathan Olsson
05/13/2022, 12:58 PM"
around it an acceptable compromise.
Agree with – let's say problematic – inconsistency from Java. LocalDate#toString
anyone? (Outputs this date as a String
, such as 2007-12-03
)ephemient
05/13/2022, 1:18 PMtoString()
is a mixed bag.
• for some types (such as Double) it produces something that looks like a representation of the value in source code or can be parsed back into the same value
• for some other types (such as most collections) it produces something which is a human-like representation of the object, but is neither the same as the source representation nor can be parsed mechanically.
• for some other types (such as Kotlin lambdas) it produces the type name but nothing to distinguish different values
• and the default is type + identity hashcodeFleshgrinder
05/13/2022, 2:23 PMtoString
. 🤔ephemient
05/14/2022, 3:27 AMList<T>
is Display
only if T
is `Display`; this isn't something that can be expressed in Kotlin. it only works for toString()
because every type implements it (ditto equals
, hashCode
which belong to traits/typeclasses in other languages that cannot be expressed in Kotlin, not that there any safe and interoperable way to do so on the JVM even if Kotlin came up with something)