Generic Functions
Generic functions can handle multiple types, not just a single fixed type. Type parameters are declared in angle brackets <T> after the function name, and can then be used in parameter types, return types, and function bodies.
Type parameters make functions more versatile. For example, pickFirst<T> takes two values of type T and returns the first one, regardless of whether T is Int64, String, or any other type. When calling, you can explicitly specify the type argument or let the compiler infer it automatically.
Cangjie supports multiple type parameters. swap<T, U> takes a (T, U) tuple and returns (U, T), with two type parameters separated by commas. This is more flexible than hardcoding specific types and avoids writing similar code repeatedly.
T, U, K, V, but longer descriptive names can also be used.Run the code to observe how pickFirst and swap work, then modify the call to swap to swap a tuple of type (Int64, Bool).