Hi, why we can't use private functions in public i...
# getting-started
g
Hi, why we can't use private functions in public inline functions?
j
Cuz inline copies the function body to wherever it's used. And you can't reference private functions from those other places
g
But doesn't it copy lambda's body to the the call site?
c
Yes, it does.
Imagine you write:
Copy code
// Module A
private val foo = 5
inline fun getFoo() = foo
and then where you use it:
Copy code
// Module B
fun main() {
    println(getFoo())
}
At compile-time, inline functions are removed; so this becomes:
Copy code
// Mobule B
fun main() {
    println(foo)
}
but
foo
is private in modula A, so this is forbidden!
r
But the
foo
variable is still there. I've always wondered why the compiler can't reference it in some hidden way?
g
but what if they are in the same class?
Copy code
class A{

    inline fun test(a:() ->Unit){
        a()
        doSomething()
    }

    private fun doSomething(){

    }
}
j
If the inline function is not private then it may be used in other classes. And then you have the same problem
g
but I am trying to use it inside
main()
function
c
@Robert Jaros I believe that would be platform-specific. On JS I believe it's possible, but probably not on the JVM?
@Gamar Mustafa is the
main
function inside the same class? If so, you can make your function private.
r
@CLOVIS You are right. I'm too much of the web developer ;)