Skip to content

Instantly share code, notes, and snippets.

@agiri23
Last active December 23, 2021 14:24
Show Gist options
  • Save agiri23/2f752c8a6554611c1f79c80c9af6a2f6 to your computer and use it in GitHub Desktop.
Save agiri23/2f752c8a6554611c1f79c80c9af6a2f6 to your computer and use it in GitHub Desktop.
Pattern Switch case
sealed interface SampleSealed permits ClassOne, ClassTwo, ClassThree {
}
final class ClassOne implements SampleSealed {
}
final class ClassTwo implements SampleSealed {
}
record ClassThree(String name) implements SampleSealed {
}
static String withSealedClass(SampleSealed sampleSealedClassApp) {
return switch (sampleSealedClassApp) {
case ClassOne classOne -> "Sample class one";
case ClassTwo classTwo -> "Sample class two";
case ClassThree classThree -> "Sample class three";
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment