Is it good practice to override inline-value class...
# getting-started
a
Is it good practice to override inline-value classes
toString
to the value? We’ve had issues where wrapping a primitive to an inline class requires checking all usages of it specially when the primitive value is already being passed directly as a String. Wondering if anyone is doing it by default.
s
Using
toString
for anything other than debugging/logging is something I try to avoid, to be honest. Better to have a separate dedicated conversion method for anything that has a specific, well-defined string representation.
2
I can just about stomach it for something like a UUID where there’s a single canonical string representation, but even then I’m on the fence
p
I'd rather UUID had a
toCanonicalRepresentation
or something slightly less verbose
1
k
Notwithstanding the advice from Sam and Phil, if you still want to override
toString
, the way you should do it should depend on what the inline class represents, rather than what it holds. For example, say you are currently holding a list of strings representing IP addresses, such as
listOf("123.45.6.7", ...)
. You decide to optimize it using an inline class holding an
Int
. You should then make the
toString
print the original (traditional) representation of the IP address rather than the Int itself.
3
👍 2