:: does in kotlin
::
in Kotlin is the notation used for creating a reference to a function or property. It is often referred to as the "function reference" or "method reference" operator. There are two main use cases for ::
in Kotlin:
- Reference to a Function: ```kotlin fun exampleFunction(message: String) { println(message) }
val functionReference: (String) -> Unit = ::exampleFunction
``
In this example,
::exampleFunctioncreates a reference to the
exampleFunctionfunction, and
functionReferenceis a variable that can be used to invoke
exampleFunctionwith a
String` parameter.
- Reference to a Property or Member: ```kotlin data class Person(val name: String, val age: Int)
val person = Person("John", 25)
val propertyReference: (Person) -> String = Person::name
``
Here,
Person::namecreates a reference to the
nameproperty of the
Personclass.
propertyReferenceis a variable that can be used to extract the
nameproperty value from a
Person` object.
In summary, ::
is used to create references to functions or properties in Kotlin, allowing you to treat them as first-class citizens and pass them around as parameters or store them in variables.