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,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"; }
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user