>>110215269
Use Java, it has algebraic data-types and pattern matching
public class Main {
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);
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);
}
};
};
};
public static void main(String[] args) {
Either<Integer, String> ea1 = Either.inl(3);
Either<Integer, String> ea2 = Either.inr("fizz");
Either<Integer, String> ea = ea1; // = ea2;
System.out.println(ea1.match(new EitherH<Integer, Integer, String>() {
public Integer left(Integer a) { return a; }
public Integer right(String b) { return b.length(); }
}));;
}
}