https://kotlinlang.org logo
#getting-started
Title
# getting-started
a

allan.conda

03/09/2022, 1:29 AM
Would it be bad practice to always override toString() to be the underlying value of an inline class? I'm not sure there's benefit to representing it as the boxed type by default. Asking because I recently made a mistake calling toString instead of the underk value
j

Joffrey

03/09/2022, 8:11 AM
toString
returns a String, while the underlying value of an inline class can be anything. You could say you want to use the
toString
of the underlying value, but it might be beneficial to override toString in a different way so you distinguish the wrapper from the inner type
👍 2
Those value classes are often used for performance/compactness as well. You might use a value class to represent a 2D index as a single 1D value
i*SIZE+j
, and in that case you would want
toString
to yield something like
(i,j)
, not the underlying 1D index
1
2 Views