Function References क्या हैं
In this page:
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
// 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)
}
Login to try C/C++/Java/PHP code in the editor
Referencing a Member Function on an Instance
instance::method उस specific object से बंधा एक member function का bound reference बनाता है, सीधे एक function value की तरह उपयोग करने योग्य।
उदाहरण: Referencing a Member Function on an Instance
// 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"))
}
Login to try C/C++/Java/PHP code in the editor
Referencing a Member Function on a Class
ClassName::method एक unbound reference बनाता है, जहां reference असल में call होने पर receiver instance को पहले argument के रूप में देना चाहिए।
उदाहरण: Referencing a Member Function on a Class
// 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")))
}
Login to try C/C++/Java/PHP code in the editor
Constructor References
::ClassName किसी class के constructor का एक reference बनाता है, जिसे बिल्कुल नए instances बनाने वाले एक function की तरह उपयोग किया जा सकता है।
उदाहरण: Constructor References
// 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)
}
Login to try C/C++/Java/PHP code in the editor
::syntax भूल जाना और सिर्फ किसी function का नाम पास करने की कोशिश करना, जिसे Kotlin इसे refer करने के बजाय call करने की कोशिश मानता है।- किसी function reference का उपयोग करना जहां referenced function का signature असल में receiving parameter की उम्मीद से मेल न खाए।
- यह न समझना कि 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: