Created
October 31, 2020 14:21
-
-
Save clarenced/f9f50cb08e665477eef1f1cc70c96a2f to your computer and use it in GitHub Desktop.
Either example with Arrow
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class DivisionByZero { | |
override fun toString(): String { | |
return "Division by zero" | |
} | |
} | |
fun divide(a: Int, b: Int): Either<DivisionByZero, Int> { | |
return try { | |
Right(a / b) | |
} catch (exception: Exception) { | |
Left(DivisionByZero()) | |
} | |
} | |
fun main() { | |
divide(10, 10).fold( | |
{ println(it)}, | |
{println("Value ${it}") } // happy path | |
) | |
divide(10, 0).fold( | |
{ println(it)}, // fail case | |
{println("Value ${it}") } | |
) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment