Function Overloading
In Cangjie, multiple functions with the same name can exist in the same scope, as long as their parameter types or parameter counts differ. This is called function overloading. The compiler automatically selects the matching function version based on the argument types passed at the call site.
Function overloading allows you to use the same name to handle different types of data, improving code readability. For example, you can define multiple print functions to handle integers, floats, and strings separately.
The example on the right defines four format functions, accepting Int64, Float64, String, and two Int64 parameters respectively. When called on lines 22-25, the compiler decides which version to call based on argument types.
Only different parameter types or counts count as overloading. Functions differing only by return type cannot be overloaded and will cause a duplicate definition error. Also, variables and functions cannot have the same name.
Try adding an overload format(value: Bool): String to handle boolean values, then call format(true) to see the effect.