if a class can become a `value class`, is there an...
# getting-started
y
if a class can become a
value class
, is there any disadvantage to doing it? assuming you already know ahead of time that you won't need additional properties.
a
for a type that’s only used within a single component? There shouldn’t be a problem, technically. If you’re writing a library it’s sometimes a good idea to hide the ‘value class’ implementation behind an interface, so that if you need to change something you can, for similar reasons to why data classes shouldn’t be used in an API https://kotlinlang.org/docs/jvm-api-guidelines-backward-compatibility.html#don-t-use-data-classes-in-an-api Value classes can also be awkward/difficult to in non-Kotlin targets, because of the name mangling https://kotlinlang.org/docs/inline-classes.html#mangling, but that’s just something to consider & test, it’s not a critical issue.
👍 1
j
Using an interface to deal with a value class almost makes the value class pointless because it will always be boxed...
💯 3
a
Yeah okay, so to clarify: I didn’t mean always using an interface. Hiding a value class from a library’s API using an interface means you could still use the value class internally. For example, if a library could have
Copy code
fun getPassword(): CharSequence
but internally the library could use a specific value class for Password.
Copy code
internal value class Password(private val value: String): CharSequence by value
👌 1