Why can't a public `inline` function call a privat...
# getting-started
j
Why can't a public
inline
function call a private
inline
function? I get why we can't call non-inline private functions (because when we inline the public function, all its body must be accessible at the call site), but that doesn't apply to other inline functions, so long as the whole inline chain ends up calling only public stuff, right? So why is this forbidden?
Copy code
inline fun doStuffInline() {
    myPrivateInline() // ERROR: Public-API inline function cannot access non-public-API 'private inline fun myPrivateInline(): Unit defined in root package in file File.kt'
}

private inline fun myPrivateInline() {
    println("Anyone can call this print statement")
}
https://pl.kotl.in/7bPhHMUJ-
i
so long as the whole inline chain ends up calling only public stuff, right
Because of that ^ This would be hard to track, unobvious how to report, and make private inline functions very fragile to changes. Suppose you have a deep chain of inlines, all of them call only public API. Now you change something in the deepest inline function so that it starts calling non-public API, and in a different part of the program, a different inline function call becomes reported as an error.
By this reason, Kotlin requires to be explicit in intent that a non-public (inline or not) function can be called from public inline functions by marking it
@PublishedApi internal
. This also places the same restrictions on marked inline functions as on public inline functions.
j
Makes sense, thanks