Co-Authored-By: Claude Sonnet 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_017ic4b6u469kis46DspusiL
15 lines
505 B
Java
15 lines
505 B
Java
/** Must NOT compile: the switch forgets Cod, and Payment is sealed, so the compiler knows. */
|
|
public class NonExhaustiveSwitch {
|
|
sealed interface Payment permits Card, Upi, Cod {}
|
|
record Card(String last4) implements Payment {}
|
|
record Upi(String vpa) implements Payment {}
|
|
record Cod() implements Payment {}
|
|
|
|
static String describe(Payment p) {
|
|
return switch (p) {
|
|
case Card c -> "card " + c.last4();
|
|
case Upi u -> "UPI " + u.vpa();
|
|
};
|
|
}
|
|
}
|