Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Type Classes Demo: Ad-hoc polymorphism without inheritance!
- interface Serializable<T> {
- String serialize(T value);
- T deserialize(String data);
- }
- interface Numeric<T> {
- T zero();
- T add(T a, T b);
- T multiply(T a, T b);
- }
- // Money type with serialization
- record Money(int dollars, int cents) {
- __witness Serializable<Money> SERIALIZER = new Serializable<>() {
- public String serialize(Money m) {
- return m.dollars() + "." + String.format("%02d", m.cents());
- }
- public Money deserialize(String s) {
- String[] parts = s.split("\\.");
- return new Money(Integer.parseInt(parts[0]), Integer.parseInt(parts[1]));
- }
- };
- __witness Numeric<Money> MATH = new Numeric<>() {
- public Money zero() { return new Money(0, 0); }
- public Money add(Money a, Money b) {
- int totalCents = (a.dollars() * 100 + a.cents()) + (b.dollars() * 100 + b.cents());
- return new Money(totalCents / 100, totalCents % 100);
- }
- public Money multiply(Money a, Money b) {
- int totalCents = ((a.dollars() * 100 + a.cents()) * (b.dollars() * 100 + b.cents())) / 100;
- return new Money(totalCents / 100, totalCents % 100);
- }
- };
- }
- // Point type with serialization
- record Point(int x, int y) {
- __witness Serializable<Point> SERIALIZER = new Serializable<>() {
- public String serialize(Point p) {
- return "(" + p.x() + "," + p.y() + ")";
- }
- public Point deserialize(String s) {
- String[] parts = s.replace("(", "").replace(")", "").split(",");
- return new Point(Integer.parseInt(parts[0]), Integer.parseInt(parts[1]));
- }
- };
- __witness Numeric<Point> MATH = new Numeric<>() {
- public Point zero() { return new Point(0, 0); }
- public Point add(Point a, Point b) {
- return new Point(a.x() + b.x(), a.y() + b.y());
- }
- public Point multiply(Point a, Point b) {
- return new Point(a.x() * b.x(), a.y() * b.y());
- }
- };
- }
- public class TypeClassDemo {
- // Demo: Money operations
- static void moneyDemo() {
- System.out.println("💰 MONEY OPERATIONS:");
- Serializable<Money> ser = Serializable<Money>.__witness;
- Numeric<Money> math = Numeric<Money>.__witness;
- Money price1 = new Money(10, 50);
- Money price2 = new Money(5, 75);
- System.out.println(" Price 1: $" + ser.serialize(price1));
- System.out.println(" Price 2: $" + ser.serialize(price2));
- Money total = math.add(price1, price2);
- System.out.println(" Total: $" + ser.serialize(total));
- // Serialize and deserialize
- String saved = ser.serialize(total);
- Money loaded = ser.deserialize(saved);
- System.out.println(" Saved & loaded: $" + ser.serialize(loaded));
- }
- // Demo: Point operations
- static void pointDemo() {
- System.out.println("\n📍 POINT OPERATIONS:");
- Serializable<Point> ser = Serializable<Point>.__witness;
- Numeric<Point> math = Numeric<Point>.__witness;
- Point p1 = new Point(3, 4);
- Point p2 = new Point(5, 6);
- System.out.println(" Point 1: " + ser.serialize(p1));
- System.out.println(" Point 2: " + ser.serialize(p2));
- Point sum = math.add(p1, p2);
- System.out.println(" Sum: " + ser.serialize(sum));
- Point product = math.multiply(p1, p2);
- System.out.println(" Product: " + ser.serialize(product));
- // Serialize and deserialize
- String saved = ser.serialize(sum);
- Point loaded = ser.deserialize(saved);
- System.out.println(" Saved & loaded: " + ser.serialize(loaded));
- }
- // Demo: Sum multiple values
- static void sumDemo() {
- System.out.println("\n➕ SUM MULTIPLE VALUES:");
- Numeric<Money> moneyMath = Numeric<Money>.__witness;
- Serializable<Money> moneySer = Serializable<Money>.__witness;
- Money[] prices = {
- new Money(10, 99),
- new Money(5, 50),
- new Money(3, 25),
- new Money(7, 10)
- };
- Money total = moneyMath.zero();
- for (Money price : prices) {
- System.out.println(" + $" + moneySer.serialize(price));
- total = moneyMath.add(total, price);
- }
- System.out.println(" ──────────");
- System.out.println(" = $" + moneySer.serialize(total));
- }
- public static void main(String[] args) {
- System.out.println("╔══════════════════════════════════════╗");
- System.out.println("║ TYPE CLASSES DEMO - VALHALLA JDK ║");
- System.out.println("╚══════════════════════════════════════╝");
- System.out.println("\nType classes enable ad-hoc polymorphism!");
- System.out.println("Same interface, different implementations per type.\n");
- moneyDemo();
- pointDemo();
- sumDemo();
- System.out.println("\n✨ No inheritance required! Type classes FTW! ✨");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment