Since inline functions cannot use non-public prope...
# stdlib
m
Since inline functions cannot use non-public properties etc, what strategy can we use to avoid exposing those properties
Copy code
inline fun <reified T: Enum<T>> Foo.enumBar(name: String): Bar<T?> = this.propertyToHide.toBar(name)
j
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.
🙏 1
m
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:
Copy code
fun <T: Enum<T>> enumValues(enumClass: KClass<out T>): Array<out T> = enumClass.java.enumConstants
j
If the enum values are what you want then that's what you should call in the inline one
m
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
Inline functions can access
internal
members that are marked with
@PublishedApi
.
🙏 1