Type Inference
When a variable has an initial value, the compiler can automatically infer the variable's type based on the initial value's type, without explicit annotation. This makes code more concise while maintaining type safety. Type inference is one of the features of Cangjie's type system.
In the code on the right, all variables lack type annotations, but the compiler knows their types: integer literals are inferred as Int64, floating-point literals as Float64, string literals as String, and boolean literals as Bool. These are Cangjie's default type inference rules. Values computed through expressions also have their types inferred.
Type inference is not dynamic typing—the variable's type is determined at compile time and doesn't change at runtime. If you try to assign a string to a variable inferred as an integer, or assign a floating-point number to a variable inferred as an integer, the compiler will report an error. Cangjie is a statically typed language, and type checking occurs at compile time.
Try adding a new binding without type annotation, such as let z = 100 + 200 or let sum = x + y, and guess what type it will be inferred as.