https://kotlinlang.org logo
Title
n

Nezteb

04/23/2019, 9:37 PM
What is the name for each of these ways to reference a function?
::funcName
funcName
funcName()
? The last one I believe is a function invocation, ie
() -> Unit
, but the first two confuse me.
a

Alowaniak

04/23/2019, 9:52 PM
The first is a function reference The second is only the name of the function, if that's already a variable pointing to a function then it's the same as the first I guess
c

Czar

04/23/2019, 10:06 PM
function (or method) reference function name function invocation
s

streetsofboston

04/23/2019, 10:49 PM
fun funcName() : Unit { ... }
::funcName
is a function reference and can be cast easily to a lambda:
val funcRef : KFunction<Unit> = ::funcName // OK
val funcLambda : () -> Unit = ::funcName // OK
val givesCompileError: () -> Unit = funcRef // compiler error
funcName
is not possible in Kotlin. This will give a syntax error. This syntax denootes either a value/variable, parameter, field, etc.
funcName()
is calling the function, executing it, returning its return value.
// These do all the exact same
funcName()
funcRef.call()
funcLambda()
n

Nezteb

04/23/2019, 11:21 PM
Sweet; thanks for all the info!