The language enforces static strong explicit type semantics. It is the most strict type system available since reliability is above everything. However, some inference abilities will not do any harm, so you do not have to specify types in places where they are highly obvious.
Type conversions can be only performed on the integer and enumeration types with the casting operator. This chapter explains the operator's behavior in detail.
let statement can perform implicit type casting of integers if the type
is specified to the left of the assignment symbol. Let us examine the statement:
let a: field = 42 as u32;
42is inferred as a value of type
42is cast from
- The expression
42 as u32result is cast to
- The field value is assigned to the variable
The second case of implicit casting is the negation operator, which always returns a signed integer type value of the same bitlength, regardless of the input argument.
let positive = 100; // u8 let negative = -positive; // i8
This chapter describes the negation operator with more detail.
For now, Zinc infers types in two cases: integer literals and
Integer literals are always inferred as values of the minimal possible size.
255 is a
u8 value, whereas
256 is a
u16 value. Signed integers
must be implicitly cast using the negation operator.
let statement can infer types in case its type is not specified.
let value = 0xffffffff_ffffffff_ffffffff_ffffffff;
In the example above, the
value variable gets type
u128, since 128 bytes
are enough to represent the value