jbgi
Re:Pattern Matching for Java
on Friday April 21, 2017 @05:41AM (#14164178)
It depends. Bare minimum, in scala you have to do:
sealed trait Either[A, B]
final case class Left[A, B](a: A) extends Either[A, B]
final case class Right[A, B](b: B) extends Either[A, B]
and it is still not a real sum type due to exposing subtyping (cf. https://github.com/quasar-analytics/quasar/wiki/Faking-Sum-T...)with derive4j:
@Data
interface Either<A, B> {
<X> X match(Function<A, X> left, Function<B, X> right);
}
or, at your preference: @Data
interface Either<A, B> {
interface Cases<X> {
X left(A leftValue);
X right(B rightValue);
}
<X> X match(Cases<X> cases);
}
So it is actually not much boilerplate.The real drawback of (any) java implementation is lack of TCO.
Re:Show HN: Derive4j – Java8 support for ADTs, pattern matching, and more
on Sunday October 18, 2015 @11:48AM (#10408525)
Derive4J: a Java 8 annotation processor for deriving algebraic data types constructors, pattern matching and more!