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 @@
|
||||
module demo.pay { }
|
||||
@@ -0,0 +1,2 @@
|
||||
package pay.api;
|
||||
public sealed interface Payment permits pay.impl.Card, pay.impl.Upi {}
|
||||
@@ -0,0 +1,10 @@
|
||||
package pay.app;
|
||||
import pay.api.Payment;
|
||||
import pay.impl.*;
|
||||
public class Main {
|
||||
public static void main(String[] args) {
|
||||
Payment p = new Card(100);
|
||||
String s = switch (p) { case Card c -> "card"; case Upi u -> "upi"; };
|
||||
System.out.println(s + " in module " + Payment.class.getModule().getName());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,2 @@
|
||||
package pay.impl;
|
||||
public record Card(long amount) implements pay.api.Payment {}
|
||||
@@ -0,0 +1,2 @@
|
||||
package pay.impl;
|
||||
public record Upi(long amount) implements pay.api.Payment {}
|
||||
Reference in New Issue
Block a user