int type.
However, at the start and end of each execution, the contract’s state is deserialized and serialized, respectively. To optimize space and reduce storage costs, it is possible to encode integer values using fewer bits.
Tolk provides additional integer types to accommodate (de)serialization:
There are also types of variable bit-width:
Literals
All the following constants are ofint type:
First-class types
All integer types can be nullable, combined within a union, and otherwise used in structural or multi-valued types:No floating-point numbers
The virtual machine supports only signed 257-bit integers. Floating-point numbers do not exist. Represent monetary, Toncoin values withcoins:
Serialization
Serialization works as follows:int— not serializable; useintNand other types.intN— a fixedN-bit signed integer.uintN— a fixedN-bit unsigned integer.coins— an alias tovaruint16.varint16— 4 bits of length followed by an 8 * length-bit number.varuint16— unsigned version ofvarint16.varint32— 5 bits of length followed by an 8 * length-bit number.varuint32— unsigned version ofvarint32.
intN describes serialization, int does not
To automatically parse binary data, the compiler must load and store integers correctly. When designing a contract schema, fields are described in terms such as “queryID is unsigned 64-bit” and “counterValue is 32-bit”. This is translates directly in Tolk:
IncMessage can be serialized to a cell and decoded back.
The general-purpose type int represents an integer with no serialization information. Consider this struct:
p of type Point. However, a call p.toCell() would produce the following error:
int with a specific integer type:
Overflow occurs only at serialization
Consider the following code:v there would neither overflow nor be clamped at runtime. Instead, it would be equal to 256 during subsequent execution steps.
There are no runtime bounds checks, and overflows of all integer types occur only during serialization, except for the general int type, which can overflow when doing arithmetic.
Generic int implicitly casts to and from any intN
All arithmetic operations on intN degrade to int and all numeric literals are of type int.
To prevent further errors, Tolk disallows direct assignments between intN and intM types, when N and M are not equal.
Type coins and function ton("0.05")
Similar to int32, Tolk has a dedicated coins type representing nanoToncoin values.
The coins type has special serialization rules. It’s serialized as variadic integer: small values consume fewer, large values consume more.
Arithmetic with coins degrades to int, similar to intN, except for addition or subtraction operations, where the coins type is preserved.
Values of type int can be cast back to coins, following the same rules as intN.
There is a ton built-in function, which calculates nanoToncoin values at compile-time. It accepts only constants and literals, e.g., ton(some_variable) is invalid.