Skip to content

Instantly share code, notes, and snippets.

@elucash
Created February 12, 2015 04:53
Show Gist options
  • Save elucash/8542f28198ca571dd666 to your computer and use it in GitHub Desktop.
Save elucash/8542f28198ca571dd666 to your computer and use it in GitHub Desktop.
Thinking of ADT-like support for Immutables. Case matching using Java8
interface Q {}
@Value.Immutable
interface A extends Q{}
@Value.Immutable
interface B extends Q{}
@Value.Immutable
interface C extends Q{ int x(); int y(); int z(); }
int result = match(q)
.caseA(a -> computeWithA(a))
.caseB(b -> computeWithB(b))
.caseC((x, y, z) -> compute(x, y, z)) // this overload takes destructured attributes
.result();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment