Behold sirs
public class Main {
// ADT
public interface EitherH<R,A,B> {
public R left(A a);
public R right(B b);
};
public interface Either<A, B> {
public <R> R match(EitherH<R,A,B> handler);
// Constructors
public static <A,B> Either<A,B> inl(final A a) {
return new Either<A,B>() {
public <R> R match(EitherH<R,A,B> handler) {
return handler.left(a);
}
};
};
public static <A,B> Either<A,B> inr(final B b) {
return new Either<A,B>() {
public <R> R match(EitherH<R,A,B> handler) {
return handler.right(b);
}
};
};
};
// Usage
public static void main(String[] args) {
// Creating
Either<Integer, String> ea1 = Either.inl(3);
Either<Integer, String> ea2 = Either.inr("fizz");
Either<Integer, String> ea = ea1; // = ea2;
// Pattern matching
System.out.println(ea.match(new EitherH<Integer, Integer, String>() {
public Integer left(Integer a) { return a; }
public Integer right(String b) { return b.length(); }
}));;
}
}
I wanted a reaction image but it's fucking impossible to find an anime girl with streams of blood coming out of her eyes from disgust at what she has seen and I lose my stash of saved images regularly