Kotlin Advanced
1. Higher Order Functions & Lambdas
```kotlin
fun operateOnNumbers(a: Int, b: Int, operation: (Int, Int) -> Int): Int {
return operation(a, b)
}
val sum = operateOnNumbers(4, 5) { x, y -> x + y }
println(sum)
```
2. Coroutines (Asynchronous Programming)
```kotlin
import kotlinx.coroutines.*
fun main() = runBlocking {
launch {
delay(1000L)
println("World!")
}
println("Hello,")
}
```
3. Sealed Classes
```kotlin
sealed class Result
class Success(val data: String): Result()
class Error(val exception: Exception): Result()
fun handleResult(result: Result) {
when (result) {
is Success -> println("Data: ${result.data}")
is Error -> println("Error: ${result.exception}")
}
}
```
4. Generics
```kotlin
class Box
val intBox = Box(10)
val strBox = Box("Hello")
```
5. DSL (Domain Specific Language)
```kotlin
fun html(init: HTML.() -> Unit): HTML {
val html = HTML()
html.init()
return html
}
class HTML {
fun body(init: Body.() -> Unit) { / ... / }
}
class Body { / ... / }
```
6. Advanced Coroutines
- Flows
- Channels
- Structured Concurrency