https://kotlinlang.org logo
Title
d

Danish Ansari

05/03/2023, 3:20 PM
Hi What is this feature called in Kotlin, where we call a function with :: ?
```fun someFunction(param: (String) -> Unit) {}

class SomeClass {
    fun functionWithOneParameter(param: String) {}
}

// caller place
// what is this :: notation called?
val someClass = SomeClass()
someFunction(param = someClass::functionWithOneParameter)
p

Paul Griffith

05/03/2023, 3:22 PM
d

Danish Ansari

05/03/2023, 3:26 PM
Thank you
v

Vampire

05/03/2023, 3:41 PM
And just to make it clear, you are not calling the function where you reference it with
::
. 🙂
d

Danish Ansari

05/03/2023, 3:44 PM
So we are just passing the reference and not actually calling it? Now I wonder if there is any performance difference or some other difference when reference with
::
and actually calling the function?
v

Vampire

05/03/2023, 3:45 PM
It's not so much about performance, it is simply a different use-case. You can for example give callbacks like that, that are then called on certain events and similar.
d

Danish Ansari

05/03/2023, 3:46 PM
Got it, thanks
p

Paul Griffith

05/03/2023, 3:59 PM
it can be thought of as syntactically cleaner, because you know for sure that you’re using another method verbatim instead of (possibly) hiding some side effect in a lambda AFAIK the performance is basically equivalent on modern JVMs, or at least not something you should really care about 99% of the time
e

ephemient

05/03/2023, 6:13 PM
the call performance is the same
the construction performance (if it's not inlined) may be different as callable references have a few additional features that lambdas don't have
s

Stephan Schröder

05/04/2023, 7:05 AM
"callable reference" 6 syllables 😬, that's probably why this is called a "tear-off" in other languages.