Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Emulated Sum Type in Java as described by Lukas Eder and Daniel Dietrich
import java.math.BigInteger;
import java.util.function.Consumer;
// http://blog.jooq.org/2016/02/16/an-ingenious-workaround-to-emulate-sum-types-in-java/
public class EmulatedSumType {
static class Tuple3<T1, T2, T3> {
final T1 v1;
final T2 v2;
final T3 v3;
public Tuple3(T1 t1, T2 t2, T3 t3) {
v1 = t1;
v2 = t2;
v3 = t3;
}
// Lots of useful stuff here
}
static <T, T1 extends T, T2 extends T, T3 extends T> void forEach(Tuple3<T1, T2, T3> tuple,
Consumer<? super T> consumer) {
consumer.accept(tuple.v1);
consumer.accept(tuple.v2);
consumer.accept(tuple.v3);
}
public static void main(final String[] bla) {
Tuple3<BigInteger, Long, Integer> tuple3 = new Tuple3<BigInteger, Long, Integer>(BigInteger.TEN, 1L, 3);
forEach(tuple3, v -> System.out.println(v.doubleValue()));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment