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:
| Name | Inclusive range | Space taken | Examples |
|---|---|---|---|
Signed intN | -2N-1 to 2N-1-1 | N bits, where N is between 1 and 257 | int32, int257, int7 |
Unsigned uintN | 0 to 2N-1 | N bits, where N is between 1 and 256 | uint16, uint256, uint119 |
| Name | Inclusive range | Space taken | Notes |
|---|---|---|---|
Unsigned coins | 0 to 2120-1 | Between 4 and 124 bits | They represent nanoToncoin, where 109 nanoToncoin equals 1 Toncoin |
Unsigned varuint16 | Same as coins | Same as coins | Rarely used |
Unsigned varuint32 | 0 to 2248-1 | Between 5 and 253 bits | Rarely used |
Signed varint16 | -2119 to 2119-1 | Same as coins | Rarely used |
Signed varint32 | -2247 to 2247-1 | Between 5 and 253 bits | Rarely used |
All these types are 257-bit integers at runtime. Overflows can occur at runtime, but they are more likely during serialization.For example, subtracting from a variable of type
uint8 does not cause a runtime overflow. Yet, attempting to store the result back to the same variable triggers exit code 5: integer out of expected range.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.