Type Patterns
Type patterns are used to match concrete types behind interfaces or abstract types. The syntax is case variable_name: Type =>. When the value's runtime type is a subtype of the specified type, the match succeeds and the value is bound to the variable.
This is especially useful when working with interface types. You can choose different handling logic based on the actual type while safely accessing type-specific members. Type patterns make runtime type checking safe and concise.
Note
Type patterns can only match subtypes of interfaces or open classes (
open class). Due to Cangjie's type system, the compiler cannot predict all possible subtypes, so typically a wildcard _ is used as a fallback at the end.Try deleting case _ => "unknown" on line 27 and see what error message the compiler gives.