Function visibility question here: I have a functi...
# getting-started
m
Function visibility question here: I have a function that is only (and should only ever be) called by other functions (all inline) in the same file. I see that it must have at least the same visibility as the calling inline function. However, I don’t want to expose that function polluting the API. Is there any way to not expose that function signature outside of the file? I was hoping something like “private inline fun…” might work, but no.
t
A public
inline fun
can only call other public functions, or internal functions annotated with
@PublishedApi
: https://kotlinlang.org/docs/reference/inline-functions.html#public-inline-restrictions
👍 2
t
Inline functions are usually just programming shortcuts and don't exist in compiled code. In that sense they don't call anything, the side which is using the inline function is actually where the calls are made. If your inline function uses lambda parameters, this brings the performance benefit of not having to wrap the lambda in an object. Also you are able to return out of the surrounding context from your lambda. However, if your functions are just inlined because you are using a reified generic parameter, you may be able to define it as a normal function that also takes a KClass parameter and just use the inline function to forward that. Anything used in the normal function can be of lower visibility.
👍 3
d
If your inline function takes a lambda and has multiple callsites to it, you'll duplicate the code of the lambda x times