https://kotlinlang.org logo
#getting-started
Title
# getting-started
m

Mark

07/11/2020, 2:17 AM
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

tseisel

07/11/2020, 5:15 AM
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

Tobias Berger

07/13/2020, 7:05 AM
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

Dico

07/13/2020, 1:16 PM
If your inline function takes a lambda and has multiple callsites to it, you'll duplicate the code of the lambda x times
2 Views