Files
java-interview-lab/experienced-2-3y/src/broken/NonExhaustiveSwitch.java
T

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();
};
}
}