sealed interface Shape {}
record Square(int x) implements Shape {}
record Rectangle(int l, int w) implements Shape {}
record Circle(int r) implements Shape {}
double getArea(Shape s) {
// Exhaustively checks for all alternatives.
return switch (s) {
case Square(var x) -> x * x;
case Rectangle(var l, var w) -> l * w;
case Circle(var r) -> Math.PI * r * r;
}
}
This is a good article: https://mccue.dev/pages/11-1-21-smuggling-checked-exceptions