https://stackoverflow.com/questions/48143268/java-tagged-union-sum-types


Haskell

data Either a b = Left a | Right b


Java

import java.util.Optional; import java.util.function.Function; public abstract class Either<A, B> { private Either() {} public abstract <C> C either(Function<? super A, ? extends C> left, Function<? super B, ? extends C> right); public static <A, B> Either<A, B> left(A value) { return new Either<>() { @Override public <C> C either(Function<? super A, ? extends C> left, Function<? super B, ? extends C> right) { return left.apply(value); } }; } public static <A, B> Either<A, B> right(B value) { return new Either<>() { @Override public <C> C either(Function<? super A, ? extends C> left, Function<? super B, ? extends C> right) { return right.apply(value); } }; } public Optional<A> fromLeft() { return this.either(Optional::of, value -> Optional.empty()); } }

ㅋㅋㅋㅋㅋㅋㅋㅋ

진짜 이렇게 까지 극단적인 예시도 나올수가 있구나;;