why doesn't this work: ```IconButton(onClick = na...
# getting-started
n
why doesn't this work:
Copy code
IconButton(onClick = navigator::popBackStack)
where navigator.popBackStack is:
Copy code
val popBackStack: () -> Unit
however, this works:
Copy code
IconButton(onClick = navigator.popBackstack)
e
isn't the message clear? if you have a
val popBackStack
, then
::popBackStack
is a reference to the val, but you want the value
if you had
fun popBackStack()
then you'd want
::popBackStack
but you don't
n
ohhhh, its clicking in my head now. the ::popBackStack essentially be returning whatever the value in popBackstack is, whereas the the other way is just invoking it, right?
e
no
there are languages in which variables and functions use the same namespace (Type-1 Lisps, Python, Javascript, etc.) and languages in which variables and functions use different namespaces (C/C++, Java, Kotlin, etc.)
while Kotlin makes it easy to call a function reference in a variable with the same syntax as a function call, it is not the same
with
fun popBackStack()
, there is no value named
popBackStack
. the only way to use it is by a function call
popBackStack()
or reference
::popBackStack
with
val popBackStack
, there is a value named
popBackStack
, not a function. Kotlin lets you write
popBackStack()
as a shortcut for
popBackStack.invoke()
❤️ 1
n
ahh thanks!