Created
August 15, 2019 11:06
-
-
Save Miha-x64/7e11fc77eef3a88f4b5c2e3032110075 to your computer and use it in GitHub Desktop.
An attachment to comment on https://www.pragmaticobjects.com/chapters/001_checked_exceptions.html
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
// Java(?) way | |
interface Fraction<E extends Exception> { | |
int numerator() throws E; | |
int denominator() throws E; | |
} | |
class FracStatic implements Fraction<RuntimeException> // don't force catching | |
class FracSum<E1, E2> implements Fraction<EitherException<E1, E2>> // impossible due to type erasure | |
// Kotlin, Scala, Rust way | |
interface Fraction<E> { | |
Either<E, Integer> numerator(); | |
Either<E, Integer> denominator(); | |
} | |
class FracStatic implements Fraction<Nothing> // return type `Either<Nothing, Integer>` means that error could not happen | |
class FracSum<E1, E2> implements Fraction<Either<E1, E2>> { // can fail in any of two ways | |
// this is ugly but possible! | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment