https://kotlinlang.org logo
Title
m

Mark

03/11/2023, 5:20 AM
Since inline functions cannot use non-public properties etc, what strategy can we use to avoid exposing those properties
inline fun <reified T: Enum<T>> Foo.enumBar(name: String): Bar<T?> = this.propertyToHide.toBar(name)
j

jw

03/11/2023, 5:22 AM
Separate the operation on the reified type from the operation on the private property. For example, you could have a non-inline overload which adds a KClass property and delegate to that.
m

Mark

03/11/2023, 6:33 AM
If I understand correctly, I would use the inline function to get the
KClass
(using
T::class
) and then use that to get the
enum
instance. So I wouldn’t be able to use
enumValueOf()
but maybe this instead:
fun <T: Enum<T>> enumValues(enumClass: KClass<out T>): Array<out T> = enumClass.java.enumConstants
j

jw

03/11/2023, 11:16 AM
If the enum values are what you want then that's what you should call in the inline one
m

Mark

03/11/2023, 1:25 PM
Ah yes, that makes much more sense. The actual code to resolve the enum is nested quite far down the call hierarchy (I don’t have the
String
I want to convert in the inline function), so instead (of passing
KClass<T>
) I now pass
toEnumOrNull: String.() -> T?
which is obtained (
String::toEnumOrNull
) in the inline function. This all works well to allow hiding of that property, but on the other hand these new overloaded funs (with the lambda arg) are now exposed. I guess there is no way around this?
e

elizarov

03/13/2023, 12:17 PM
Inline functions can access
internal
members that are marked with
@PublishedApi
.