← Back to Kotlin Course | Chapter 9: Lambdas & Higher-Order Functions | Lesson 5 of 6

Function References क्या हैं

एक function reference आपको किसी पहले से मौजूद function को नाम से इशारा करने देता है और इसे दो colons का उपयोग करके एक value की तरह सौंपने देता है।
Syntax
kotlin
list.filter(::topLevelFunction)          // top-level function
list.forEach(instance::method)           // bound to an instance
list.map(ClassName::method)              // receiver comes from each element

Referencing a Top-Level Function

::functionName किसी मौजूदा standalone function को refer करता है, इसे किसी lambda में wrap किए बिना, जहां भी एक matching function type की उम्मीद हो वहां सीधे पास होने देते हुए।

उदाहरण: Referencing a Top-Level Function

markup
// Define the function `isEven` taking `n` and returning a `Boolean`
fun isEven(n: Int): Boolean = n % 2 == 0

// Entry point: execution of the program starts here
fun main() {
    // Declare a read-only variable `numbers`, set to `listOf(1, 2, 3, 4, 5, 6)`
    val numbers = listOf(1, 2, 3, 4, 5, 6)
    // Declare a read-only variable `evens`, set to `numbers.filter(::isEven)`
    val evens = numbers.filter(::isEven)
    // Print `evens` to the console, with a trailing newline
    println(evens)
}

Referencing a Member Function on an Instance

instance::method उस specific object से बंधा एक member function का bound reference बनाता है, सीधे एक function value की तरह उपयोग करने योग्य।

उदाहरण: Referencing a Member Function on an Instance

markup
// Define a class `Greeter`
class Greeter(val greeting: String) {
    fun greet(name: String) = "$greeting, $name!"
}

// Entry point: execution of the program starts here
fun main() {
    // Declare a read-only variable `greeter`, set to `Greeter("Hi")`
    val greeter = Greeter("Hi")
    // Declare a read-only variable `greetFn` (a `(String) -> String`), set to `greeter::greet`
    val greetFn: (String) -> String = greeter::greet
    // Print `greetFn("Kotlin")` to the console, with a trailing newline
    println(greetFn("Kotlin"))
}

Referencing a Member Function on a Class

ClassName::method एक unbound reference बनाता है, जहां reference असल में call होने पर receiver instance को पहले argument के रूप में देना चाहिए।

उदाहरण: Referencing a Member Function on a Class

markup
// Define a data class `Person`
data class Person(val name: String) {
    fun shout() = name.toUpperCase()
}

// Entry point: execution of the program starts here
fun main() {
    // Declare a read-only variable `shoutFn` (a `(Person) -> String`), set to `Person::shout`
    val shoutFn: (Person) -> String = Person::shout
    // Print `shoutFn(Person("ana"))` to the console, with a trailing newline
    println(shoutFn(Person("ana")))
}

Constructor References

::ClassName किसी class के constructor का एक reference बनाता है, जिसे बिल्कुल नए instances बनाने वाले एक function की तरह उपयोग किया जा सकता है।

उदाहरण: Constructor References

markup
// Define a data class `Point`
data class Point(val x: Int, val y: Int)

// Entry point: execution of the program starts here
fun main() {
    // Declare a read-only variable `coordinates`, set to `listOf(1 to 2, 3 to 4)`
    val coordinates = listOf(1 to 2, 3 to 4)
    // Declare a read-only variable `points`, set to `coordinates.map { (x, y) -> Point(x, y) }`
    val points = coordinates.map { (x, y) -> Point(x, y) }
    // Print `points` to the console, with a trailing newline
    println(points)
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. :: syntax भूल जाना और सिर्फ किसी function का नाम पास करने की कोशिश करना, जिसे Kotlin इसे refer करने के बजाय call करने की कोशिश मानता है।
  2. किसी function reference का उपयोग करना जहां referenced function का signature असल में receiving parameter की उम्मीद से मेल न खाए।
  3. यह न समझना कि member function references को यह निर्भर करते हुए कि वे कैसे call होंगे, या तो एक bound receiver (instance::method) या एक unbound (ClassName::method) चाहिए।
चैप्टर सारांश
  • ::functionName किसी मौजूदा top-level या member function का एक reference बनाता है, जिसे जहां भी एक matching function type की उम्मीद हो वहां पास किया जा सकता है।
  • ClassName::method एक unbound reference बनाता है, call होने पर receiver को पहले parameter के रूप में उम्मीद करते हुए।
  • instance::method उस specific instance से बंधा एक bound reference बनाता है।
  • Function references मौजूदा functions को किसी lambda में wrap किए बिना सीधे higher-order functions को पास करने के लिए उपयोगी हैं।
🔒

Chapter Quiz — Complete all 6 topics to unlock

0/6 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.