Are there any shortcuts for exposing only specific...
# getting-started
d
Are there any shortcuts for exposing only specific methods and properties of a value class? It gets a bit tedious threading through the calls to underlying methods
j
I am not following you. Is any visibility modifier not enough?
private
, etc
d
I am talking about exposing the functions and properties of the underlying type of an inline value class. Currently to expose the methods I want I need to write the same signature and pass it through to call the wrapped type.
c
The point of a value class is to be a different type with a clean API, so no, not by default. However, if what you want to expose behavior from an
interface
, there is delegation:
Copy code
value class Username(
    private val value: Whatever,
) : Foo by value
If you want to automatically have access to all functionality of the underlying type, use
typealias
instead
d
Does that mean it will delegate all the declarations in
Foo
fulfilled by
value
?
j
Wouldn't break this boxing?
d
Perhaps… but actually Delegation is apparently something I missed in my read trough of the docs and this seems like what I want (though to your point I may promote my value class to a data class in)
c
yes For boxing: depends on how you're using. If you use the object directly, it shouldn't. If you upcast to the interface, then yeah.
👍 1