We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 81ecf76 commit 8a86ba8Copy full SHA for 8a86ba8
src/main/kotlin/com/hoc081098/kotlin_playground/arrowkt/catch.kt
@@ -0,0 +1,28 @@
1
+package com.hoc081098.kotlin_playground.arrowkt
2
+
3
+import arrow.core.raise.Raise
4
+import arrow.core.raise.catch
5
+import arrow.core.raise.either
6
7
+data class User(val id: Int, val name: String)
8
+sealed interface Error {
9
+ data class UserNotFound(val id: Int) : Error
10
+}
11
12
+fun findUserById(id: Int): User =
13
+ throw NoSuchElementException("User'$id' not found")
14
15
+context(Raise<Error.UserNotFound>) fun findUserByIdWithRaise(id: Int): User =
16
+ catch(
17
+ block = { findUserById(id) },
18
+ catch = {
19
+ if (it is NoSuchElementException)
20
+ raise(Error.UserNotFound(id))
21
+ else
22
+ throw it
23
+ }
24
+ )
25
26
+fun main() {
27
+ println(either { findUserByIdWithRaise(1) })
28
0 commit comments