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 default type of an integer literal is i32
.
If you need the literal to be of a different type you either need to
specify the type of the field or use the corresponding constructor.
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.