Functional Error Handling
Map and recover.
Errors as Data Flows
Functional error handling treats failures as values you can transform, chain, and recover from — just like you transform successful values. The built-in Result offers operators for exactly this.
map
map transforms a successful value and leaves a failure untouched, propagating it unchanged.
fun main() {
val r = Result.success(5).map { it * 2 }
println(r.getOrNull())
val f = Result.failure<Int>(Exception("x")).map { it * 2 }
println(f.getOrNull())
}All lessons in this course
- The Result Type
- runCatching
- Sealed Result Hierarchies
- Functional Error Handling