Fuzion Logo
fuzion-lang.dev — The Fuzion Language Portal
JavaScript seems to be disabled. Functionality is limited.

Integer Literals

Plain

Integer constants are sequences of digits. A prefix + or - can be added to set the sign. Integers can be assigned to variables of integer types: i8, i16, i32, i64, u8, u16, u32 and u64.

Overflows

Values that do not fit into the type they are assigned to will cause a compile time error:

Grouping

Digits in a constant may be grouped using underscore characters _. The underscores must create groups of digits of equal sizes, only the first group can be smaller. This grouping can be used to improve readability, e.g., to use groups of three to separate thousands, millions, etc., in decimal numbers or to group bytes in binary numbers:

Groups of different sizes will cause a compilation error:

Bases

Integer constants can be given in decimal, binary, octal or hexadecimal representation. A prefix 0d, 0b, 0o or 0x, respectively, selects the base to be used. If no prefix is given, decimal representation using base 10 is used as default.

Exponents

Integer constants may use an exponent. Exponents are introduced using E or P, where E introduces an exponent to base 10, while P introduces an exponent to base 2.

Fractional Parts

Integer constants in Fuzion may come with a fractional part, as long as the resulting number is an integer.

If the resulting number is a fraction, it cannot be used as an integer:

Types

The type of an integer literal that is assigned to a field is inferred from that field. If the field, however, has itself a type that is inferred from the value assigned to it, the type of the integer literal depends on its value: A value that fits into type i32 will have that type, any other value will be of type i64.

When performing calculations with integer literals using infix operators, the type of the calculation depends on the left operand:

To explicitly choose a type for a literal, place the name of the type in front of it as in u64 1:

When adding the type explicitly before the integer literal, you actually call the constructor of that type given the constant as an argument. All built-in numeric types have a constructor with one argument val that has the same type. Consequently, the type inference mechanism forces that type onto the constant.