Skip to content

Instantly share code, notes, and snippets.

@Miha-x64
Created August 15, 2019 11:06
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save Miha-x64/7e11fc77eef3a88f4b5c2e3032110075 to your computer and use it in GitHub Desktop.
Save Miha-x64/7e11fc77eef3a88f4b5c2e3032110075 to your computer and use it in GitHub Desktop.
// 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