Add sealed module: modelling domains with sealed types and exhaustive switch
Payment model, permits and the three modifiers, default-branch trap, generic and recursive hierarchies, visitor comparison, run-time enforcement, modules. Transcripts from JDK 21, 25 and 27. Co-Authored-By: Claude Sonnet 5 <[email protected]> Claude-Session: https://claude.ai/code/session_01TF9JWFvJSNm6HVzswzZU5a
This commit is contained in:
@@ -0,0 +1,16 @@
|
||||
public class WalletNotHandled {
|
||||
sealed interface Payment permits Card, Upi, NetBanking, Wallet {} // Wallet added
|
||||
record Card(long amount) implements Payment {}
|
||||
record Upi(long amount) implements Payment {}
|
||||
record NetBanking(long amount) implements Payment {}
|
||||
record Wallet(long amount) implements Payment {}
|
||||
|
||||
// Same switch as before, with no default and no case for Wallet.
|
||||
static long fee(Payment p) {
|
||||
return switch (p) {
|
||||
case Card c -> c.amount() * 2 / 100;
|
||||
case Upi u -> 0;
|
||||
case NetBanking n -> 10;
|
||||
};
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user