The Any Type
Any is Cangjie's top type, and all types implement it by default. Any is equivalent to an empty interface—it can hold values of any type, including basic types, custom types, and even other interface types.
You can use type pattern matching to recover concrete types from Any values. During matching, each type branch is checked in order, and after a successful match, the variable is bound and the corresponding code block is executed.
The Any type is suitable for handling dynamic data, heterogeneous collections, or scenarios where types are uncertain at compile time. However, overusing Any loses the advantages of type safety—concrete types or generics should be preferred.
In this example, the printValue function accepts an Any parameter and uses type pattern matching to recover the concrete type and perform different operations. The Box class uses Any to store arbitrary values.
Add a Float64 branch to the match in printValue and test it by calling printValue(3.14).
Any provides flexibility, it should be used cautiously. Prefer generics or interfaces, and only use Any when you truly need to handle arbitrary types.