inlinekotlinpublicjava-synthetic-methods

Inline function cannot access non-public-API: @PublishedApi vs @Suppress vs @JvmSynthetic


In Kotlin, when I have a non-public member and an inline fun that calls it, there's a compilation error saying:

Error:(22, 25) Kotlin: Public-API inline function cannot access non-public-API private fun f(): Unit defined in com.example

I found several ways to call my function inside a public inline fun, but which is the best way to do it?

Suppose I have a private fun f() { }. Then the options I found are:

So, which of these solutions is the best way call a non-public function from a public inline one? What are the downsides of each solution that I don't see?


Solution

  • @PublishedApi internal is the intended way of exposing non-public API for use in public inline functions.

    That @PublishedApi internal member becomes effectively public and its name doesn't get mangled (if you noticed the opposite, please file a bug).

    @Suppress("NON_PUBLIC_CALL_FROM_PUBLIC_INLINE") is a band-aid workaround in the lack of @PublishedApi based on suppressing an error and therefore isn't recommended. With the introduction of @PublishedApi this suppression is going to be cleaned from the standard library.

    @JvmSynthetic combined with @PublishedApi is an interesting approach, however it can cause some problems while debugging, though I'm not sure.