Why architecture matters here
Pattern matching fails when misused — as a replacement for polymorphism or without sealed hierarchies. Architecture matters because sealed + exhaustive is what makes pattern matching safe and refactorable.
The architecture: every piece explained
The top strip is primitives. Sealed interface closes hierarchy. Records are data carriers. Switch expression returns a value. Type patterns — instanceof with capture.
The middle row is depth. Record patterns deconstruct. Guards (when) add conditional. Exhaustiveness compiler-verified for sealed. Nested patterns match inside inside.
The lower rows are practice. Refactoring class → sealed + record. Interop with visitor + polymorphism. Ops — style + team adoption.
End-to-end flow
End-to-end: `sealed interface Shape permits Circle, Rect, Triangle {}` — each is a record. `switch(shape) { case Circle(var r) -> ...; case Rect(var w, var h) -> ...; case Triangle(var a, var b, var c) -> ...; }` — exhaustive. Compiler catches unhandled case if you add Pentagon.