In Kotlin, when I have a non-public member and inline fun that calls it, there is a compilation error:
Error: (22, 25) Kotlin: the built-in Public-API function cannot access the private fun f(): Unit non-public API defined in com.example
I found several ways to call my function inside the public inline fun , but is this the best way to do this?
Suppose I have private fun f() { } . Then the options I found are as follows:
fun f() { }
Just make it publicly available. This is a basic solution, but if others get serious flaws, it might be the best.
@PublishedApi internal fun f() { }
Introduced in Kotlin 1.1-M04 , the annotation can be applied to the internal member, making it effectively public. The implication I noticed is that any user of the library can still call it from Java code, which I don't like about it.
@Suppress("NON_PUBLIC_CALL_FROM_PUBLIC_INLINE") inline fun g() { f() }
Found in stdlib sources , this annotation seems to suppress the error when applied to the calling function. But what are its limitations? Can it be used only for inline functions? Does the program hurt in some cases? I tried to call a non-built-in function from the built-in with this trick, and it worked, but it looks suspicious.
@JvmSynthetic @PublishedApi internal fun f() { }
Combine the second solution with a synthetic flag in bytecode. I'm not sure if this is the correct use of @JvmSynthetic , but it hides the function from Java code, which solves the @PublishedApi internal problem.
So, which of these solutions is the best way to call a non-public function from a public inline? What are the disadvantages of every solution that I do not see?
public inline kotlin
hotkey
source share