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()); } }ㅋㅋㅋㅋㅋㅋㅋㅋ
진짜 이렇게 까지 극단적인 예시도 나올수가 있구나;;
저것도 익명클래스로 줄여서 저정도지 Left랑 Right 서브클래스 직접 생성하면 2배로 불어남
생성 - 선언
그러게. java 안해서 익명클래스란게 있는것도 이 코드 보고 처음 앎
예전 좆바는 클로저를 저걸로 구현했음 ㅋㅋ
C++도 그랬잖아 ㅋㅋㅋㅋ operator()
저게 가능한지도 몰랐다. 근데 사실상 쓰지 말라는 걸지도.