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,7 @@
|
||||
public class AnonymousSealed {
|
||||
sealed interface Payment permits Card {}
|
||||
record Card(long amount) implements Payment {}
|
||||
static Payment sneaky() {
|
||||
return new Payment() {}; // an anonymous class is a subtype nobody listed
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
public class MissingModifier {
|
||||
sealed interface Payment permits Card {}
|
||||
class Card implements Payment {} // must say final, sealed or non-sealed
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
public class NoSubclass {
|
||||
sealed interface Payment {} // no permits clause and no subtype in this file
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
public class NonSealedWrongPlace {
|
||||
interface Plain {}
|
||||
non-sealed class Oops implements Plain {} // non-sealed only makes sense under a sealed supertype
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
public class NotPermitted {
|
||||
sealed interface Payment permits Card, Upi {}
|
||||
record Card(long amount) implements Payment {}
|
||||
record Upi(long amount) implements Payment {}
|
||||
record Cheque(long amount) implements Payment {} // not in the permits clause
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
public class VisitorAdded {
|
||||
interface Payment { <R> R accept(Visitor<R> v); }
|
||||
interface Visitor<R> { R visitCard(Card c); R visitUpi(Upi u); R visitWallet(Wallet w); } // Wallet added here
|
||||
record Card() implements Payment { public <R> R accept(Visitor<R> v) { return v.visitCard(this); } }
|
||||
record Upi() implements Payment { public <R> R accept(Visitor<R> v) { return v.visitUpi(this); } }
|
||||
record Wallet() implements Payment { public <R> R accept(Visitor<R> v) { return v.visitWallet(this); } }
|
||||
|
||||
// Two existing operations, neither knows about Wallet: each one now fails to compile.
|
||||
static class Fee implements Visitor<Long> {
|
||||
public Long visitCard(Card c) { return 2L; }
|
||||
public Long visitUpi(Upi u) { return 0L; }
|
||||
}
|
||||
static class Label implements Visitor<String> {
|
||||
public String visitCard(Card c) { return "card"; }
|
||||
public String visitUpi(Upi u) { return "upi"; }
|
||||
}
|
||||
}
|
||||
@@ -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;
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,2 @@
|
||||
package a;
|
||||
public sealed interface Base permits b.Impl {}
|
||||
@@ -0,0 +1,2 @@
|
||||
package b;
|
||||
public final class Impl implements a.Base {}
|
||||
Reference in New Issue
Block a user