What is the name for each of these ways to referen...
# announcements
n
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
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
function (or method) reference function name function invocation
s
fun funcName() : Unit { ... }
::funcName
is a function reference and can be cast easily to a lambda:
Copy code
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.
Copy code
// These do all the exact same
funcName()
funcRef.call()
funcLambda()
n
Sweet; thanks for all the info!