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

Nick

06/05/2023, 6:10 PM
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

ephemient

06/05/2023, 6:18 PM
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

Nick

06/05/2023, 6:20 PM
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

ephemient

06/05/2023, 6:20 PM
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

Nick

06/05/2023, 6:34 PM
ahh thanks!
2 Views