How to pass a function as a parameter to another function in Kotlin?
Hi! I've just started learning Kotlin after Java and ran into a problem.
In Java, I'm used to using interfaces or functional interfaces (e.g., `Consumer`, `Runnable`) to pass behavior into methods. In Kotlin, as I understand, functions are first-class objects and can be passed directly.
But I can't figure out the correct syntax. Let's say I have a simple list of numbers, and I want to write a function `processNumbers` that takes a list and a handler function for each element.
Here's what I'm trying to do:
```kotlin
fun processNumbers(numbers: List, operation: ???) {
for (number in numbers) {
operation(number)
}
}
fun main() {
val numbers = listOf(1, 2, 3, 4, 5)
// I want to pass a function here that...
```
No answers yet.
Log in to leave an answer